64 lines
1.5 KiB
C
64 lines
1.5 KiB
C
#include <liquid/liquid.h>
|
|
#include <math.h>
|
|
#include <complex.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
const float SAMPLINGRATE = 1000000.0;
|
|
const float TEST_TONE = 0.0;
|
|
|
|
const int FFT_SIZE = 1024;
|
|
|
|
int main() {
|
|
|
|
nco_crcf test_tone = nco_crcf_create(LIQUID_NCO);
|
|
nco_crcf_set_phase(test_tone, 0.0f);
|
|
float freq = (2 * M_PI * TEST_TONE) / SAMPLINGRATE;
|
|
nco_crcf_set_frequency(test_tone, freq);
|
|
|
|
|
|
// allocated memory arrays
|
|
float complex * x = (float complex*) malloc(FFT_SIZE * sizeof(float complex));
|
|
float complex * y = (float complex*) malloc(FFT_SIZE * sizeof(float complex));
|
|
|
|
// create FFT plan
|
|
fftplan fft = fft_create_plan(FFT_SIZE, x, y, LIQUID_FFT_FORWARD, 0);
|
|
|
|
for(int i = 0; i < FFT_SIZE; i++) {
|
|
nco_crcf_step(test_tone);
|
|
nco_crcf_cexpf(test_tone, &x[i]);
|
|
}
|
|
|
|
fft_execute(fft);
|
|
|
|
float mag_max = cabsf(y[0]);
|
|
for(int i = 0; i < FFT_SIZE; i++) {
|
|
float mag = cabsf(y[i]);
|
|
if(mag > mag_max) {
|
|
mag_max = mag;
|
|
}
|
|
}
|
|
|
|
for(int i = 0; i < FFT_SIZE; i++) {
|
|
float mag = cabsf(y[i]);
|
|
mag = mag / mag_max;
|
|
|
|
float freq;
|
|
if(i < FFT_SIZE/2) {
|
|
freq = i * SAMPLINGRATE / FFT_SIZE;
|
|
} else {
|
|
freq = (i-FFT_SIZE/2) * SAMPLINGRATE / FFT_SIZE - SAMPLINGRATE/2;
|
|
}
|
|
printf("%f %f\n", freq, mag);
|
|
}
|
|
|
|
|
|
nco_crcf_destroy(test_tone);
|
|
fft_destroy_plan(fft);
|
|
|
|
free(x);
|
|
free(y);
|
|
|
|
return 0;
|
|
}
|