I tried to apply FFT from random audio file (will input by user) by using FFTW.
Here is my code:
void window(double in[], double out[], int N){
for (int i = 0; i < N; i++) {
double multiplier = 0.5 * (1 - cos(2*M_PI*i/(N - 1)));
out[i] = multiplier * in[i];
}
}
int main (int argc, char * argv []) {
char *infilename ;
SNDFILE *infile = NULL ;
FILE *outfile = NULL ;
SF_INFO sfinfo ;
infile = sf_open("test.wav", SFM_READ, &sfinfo);
int N = pow(2, 10); // FFT size
double samples[N];
double samples_windows[N];
// read into samples
sf_read_double(infile, samples, N);
fftw_complex out[N];
fftw_plan p;
// apply hann Windows function
window(samples, samples_windows, N);
// create 1D plan
p = fftw_plan_dft_r2c_1d(N, samples_windows, out, FFTW_ESTIMATE);
// excute FFT
fftw_execute(p);
fftw_destroy_plan(p);
printf("Real Img\n\n");
for (int i=0; i<N; i++) {
printf("%f %f\n", out[i][0], out[i][1]);
}
double magnitude[N];
for (int i=0; i<N; i++) {
printf("%i %f\n", i, sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]));
magnitude[i] = sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1]);
}
double max = max_array(magnitude, N); // find max value in array
printf("Most occurring frequencies = %f", max);
sf_close (infile) ;
return 0 ;
}
- By this way, I can get the most occurring frequencies. But how can I get the frequencies and time intervals of each value instead of magnitude?
- How can I know the sample rate of input audio? According to this answer , sample rate is required for getting frequency.