STM32 - cortex-m4 fft problem

Discussion in 'Embedded Systems and Microcontrollers' started by tuko maliba, Jul 26, 2016.

  1. tuko maliba

    Thread Starter New Member

    Jul 9, 2016
    3
    0
    Hey,
    i want to make fft of signal on STM32 discovery board with CORTEX-M4 core. I look at example and write some code. It works with their signal. But when i test it with my test signal, generated in matlab
    i have problem. I generate test signals with sampling frequency 20kHz. Lenght of test signal is 2048 samples and lenght of fft is 1024. I get 1024 values, but only first 512 values are
    credibly. Is it normal that 512. value respresent frequency 5kHz?
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    It doesn't matter that you are using CORTEX-M4 on a STM32 Discovery board.
    However it might helpful to know which FFT code or library function you are using.

    You should be aware of the following.

    FFT works with real and imaginary data arrays.
    It is customary to fill the real input array with sampled data and set the imaginary input array to zero.
    Or you can also fill the imaginary input array with additional real data.

    The FFT will generate real and imaginary results. To compute the power spectrum, add the square of the real part to the square of the imaginary part. You will end up with the power spectrum consisting of both positive and negative frequencies. The first half of the spectrum will be positive frequencies from 0Hz to the Nyquist frequency. This will be followed by the negative frequencies from -fNYQUIST to -0Hz. Since the negative frequencies are mirror images of the positive frequencies you can discard this part of the spectrum.

    Let's look at the math.

    Your sampling frequency = fs = 20kHz
    Your sampling period = ts = 1/fs = 50μs
    Number of samples = N = 2048
    Sample record = N x ts = N/fs = 2048/20kHz = 0.1024 s
    Frequency resolution = 1/(Sample record) = fs/N = 9.77 Hz
    Frequency at 512th data point in spectrum = 512 x fs/N = 5kHz
    Frequency at 1024th data point in spectrum = 1024 x fs/N = 10kHz = fNYQUIST

    Hence your answer is yes, 512th value is 5kHz.
    Your spectrum should be 1024 points to show frequencies from 0Hz to fNYQUIST
     
Loading...