FFT works

This commit is contained in:
Sebastian 2021-12-12 18:24:36 +01:00
parent 5bbb5cbdbc
commit 1201e2fdf3
4 changed files with 86 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.raw
*.py
*.csv

2
fft-beacon-finder/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
finder
!plot.py

View File

@ -0,0 +1,67 @@
#include <stdlib.h>
#include <liquid/liquid.h>
#include <math.h>
#include <complex.h>
#include <stdio.h>
const float SAMPLINGRATE = 1000000.0;
const float FFT_LEN = 256;
int main() {
FILE* output = fopen("output.csv", "w");
FILE* input = fopen("input.raw", "r");
float complex * fft_in = (float complex*) malloc(FFT_LEN * sizeof(float complex));
float complex * fft_out = (float complex*) malloc(FFT_LEN * sizeof(float complex));
// create FFT plan
fftplan fft = fft_create_plan(FFT_LEN, fft_in, fft_out, LIQUID_FFT_FORWARD, 0);
int pos = 0;
float in[2];
while(fread(in, sizeof(float), 2, input) == 2) {
fft_in[pos] = (in[1] + I * in[0]);
pos += 1;
if(pos == FFT_LEN) {
pos = 0;
fft_execute(fft);
float fft_min = cabsf(fft_out[0]);
float fft_max = cabsf(fft_out[0]);
for(int i = 0; i < FFT_LEN; i++) {
float mag = cabsf(fft_out[i]);
if(mag < fft_min) {
fft_min = mag;
}
if(mag > fft_max) {
fft_max = mag;
}
}
//printf("Min: %f Max: %f\n", fft_min, fft_max);
for(int i = FFT_LEN / 2; i < FFT_LEN; i++) {
float mag = cabsf(fft_out[i]);
mag = (mag - fft_min) / (fft_max - fft_min);
fprintf(output, "%f;", mag);
}
for(int i = 0; i < FFT_LEN / 2; i++) {
float mag = cabsf(fft_out[i]);
mag = (mag - fft_min) / (fft_max - fft_min);
fprintf(output, "%f;", mag);
}
fprintf(output, "\n");
}
}
fft_destroy_plan(fft);
free(fft_in);
free(fft_out);
fclose(output);
fclose(input);
return 0;
}

16
fft-beacon-finder/plot.py Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env python3
from numpy import genfromtxt
import matplotlib
import matplotlib.pyplot as plt
def main():
data = genfromtxt('output.csv', delimiter=';')
fig, ax = plt.subplots()
im = ax.imshow(data)
plt.show()
if __name__ == '__main__':
main()