Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DFT function implementation in C

Tags:

c

fft

I am working on implementation of BFSK implementation on a DSP processor and am currently simulating it on a LINUX machine using C. I am working on the demodulation function and it involves taking a FFT of the incoming data. For simulation purposes, I have a pre-defined function for DFT which is:

void dft(complex_float* in, complex_float* out, int N, int inv)
{
    int i, j;
    float a, f;
    complex_float s, w;
    f = inv ? 1.0/N : 1.0;
    for (i = 0; i < N; i++) {
        s.re = 0;
        s.im = 0;
        for (j = 0; j < N; j++) {
            a = -2*PI*i*j/N;
        if (inv) a = -a;
        w.re = cos(a);
        w.im = sin(a);
        s.re += in[j].re * w.re - in[j].im * w.im;
        s.im += in[j].im * w.re + in[j].re * w.im;
    }
    out[i].re = s.re*f;
    out[i].im = s.im*f;
}

Here the complex_float is a struct defined as follows:

typedef struct {
 float re;
 float im;
} complex_float;

In the dft() function, the parameter N denotes the number of DFT points.

My doubt is that since the algorithm also involves a frequency hopping sequence, while demodulating the signal, I need to check the amplitude of DFT of the signal at different frequency components.

In MATLAB this was quite simple as the FFT function there involves the sampling frequency as well and I could find the power at any frequency point as

powerat_at_freq = floor((freq * fftLength) / Sampling_freq)

But the C function does not involve any frequencies, so how can I determine the magnitude of the DFT at any particular frequency?

like image 796
anshu Avatar asked Apr 15 '26 04:04

anshu


2 Answers

The index in the FFT table for a particular frequency is calculated as follows:

int i = round(f / fT*N)

where f is the wanted frequency, fT is the sampling frequency and N is the number of FFT points. The FFT should be fine-grained enough (i.e. N should be large) to cover all the frequencies. If the precise frequency isn't present in the FFT, the nearest one will be used. More info about FFT indexes versus frequencies:

How do I obtain the frequencies of each value in an FFT?

like image 60
Ilmo Euro Avatar answered Apr 16 '26 17:04

Ilmo Euro


The frequency represented depends on the sample rate of the data fed to it (divided by the length if the FFT). Thus any DFT or FFT can represent any frequency you want just by feeding it the right amount of data at the right sample rate.

like image 27
hotpaw2 Avatar answered Apr 16 '26 17:04

hotpaw2