Skip to content

Instantly share code, notes, and snippets.

@2ndMessiah
Created March 3, 2023 09:00
Show Gist options
  • Save 2ndMessiah/03f875c8fba6f790cdefebb187c37c9e to your computer and use it in GitHub Desktop.
Save 2ndMessiah/03f875c8fba6f790cdefebb187c37c9e to your computer and use it in GitHub Desktop.
c snippet
#include "fft.h"
#include <stdlib.h>
#include <stdio.h>
#include <complex.h>
#include <math.h>
#include <time.h>
#define pi 3.14159265358979323846
int main(int argc, char* argv[]) {
int n=24;
double index[n];
double signal[n];
double complex result[n];
double modulous[n];
double degree[n];
double i; //second
int j; //index
double fs = 1200;//frequency of sampling
double time;
//time index generation
for (i=0,j=0;j<(n);j++)
{
index[j]=i;
signal[j]=2*sin(2*pi*50.0*i);
i+= (1)/fs;
}
time0=clock();
fft_slow(signal,result,n);
printf("FFT takes %f ms\n\n",clock()-time0);
for (j=0;j<(n);j++)
{
if (j==0)
{
modulous[j]=cabs(result[j])/n;
}
else if (j==n/2+1)
{
modulous[j]=cabs(result[j])/n;
}
else
{
modulous[j]=cabs(result[j])*2/n;
}
printf("freq:%f value:%f\n",j*50.0,modulous[j]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment