cancel
Showing results for 
Search instead for 
Did you mean: 

Cosine function giving unexpected result

NicRoberts
Senior II

NUCLEO F411RE, CubeIDE

As a learning exercise I'm trying to generate 100Hz cosine wave & then eventually do a DFT on it but I'm getting some odd results from the cos function using code below...

 
#include "arm_math.h"

#define NUM_SAMP 100
#define SIGNAL_FREQ 100.0
#define SAMPLING_FREQ 1000

static void fpu_enable(void);

typedef struct
{
  float real;
  float imag;
}complex_number;

complex_number samples[NUM_SAMP];

main()
{
  fpu_enable();
  /* Generate a test signal 100Hz */
  for(int n = 0; n < NUM_SAMP; n++ )
   {
     samples[n].real = cos(2 * PI* SIGNAL_FREQ * n / SAMPLING_FREQ);
     samples[n].imag = 0.0;
   }
}

static void fpu_enable(void)
{
  SCB->CPACR |= ((3UL << 10 * 2) | (3UL << 11 * 2));
}
 

 

When I check the values in samples in the debugger....

weird_cos.png

I was expecting a value of 1 for the first entry and then subsequent values in the interval [-1, 1]

Is this a formatting issue in Cube's debugger or am I missing something obvious here?

 

1 REPLY 1
mfgkw
Associate III

arm_math.h is for fixed point values or 32 bit floating point functions arm_sin_cos_q31() and arm_sin_cos_f32().

cos() is a double precision function from math.h.

(1) Do you really want to compute in double precision?

(2) Did you include math.h?

 

 

P.S.: The floating point unit on your STM32F411RET6 does not support double precision, only single precision (32 bit values).