anshu
anshu

Reputation: 665

DFT function implementation in C

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?

Upvotes: 3

Views: 8229

Answers (3)

hotpaw2
hotpaw2

Reputation: 70733

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.

Upvotes: 1

Terence Xie
Terence Xie

Reputation: 185

You can refer to the FFTW library which is famous and useful in the applicational area of FFT.

The official website is: http://www.fftw.org/

By the way, the matlab's FFT function is also implemented through the FFTW library.

Upvotes: 0

Ilmo Euro
Ilmo Euro

Reputation: 5165

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?

Upvotes: 3

Related Questions