I have been googling extensively and could plot the FFT of my wav file using Python but am unable to do so for C++, which I originally had to do. I downloaded and linked the FFTW and LIBSND to Visual C++. Though I am not understanding which functions to use in the library and what to send to compute the same kind of result as I did in Python.
Question : I basically first am not understanding, how to get the arrays of Amplitude, Frequency and then I'll go ahead and plot them.
My C++ code for the same is : `
//.........................np.fft.fft as in python
p = fftw_plan_dft_1d(num_items, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute(p);
//According to Nyquist its 1/2
for (int i = 0; i < num_items / 2; ++i) {
printf("real=%f ",out[i][0]);
printf("img=%f ",out[i][1]);
}
//Amplitude
float *amp;
amp = (float *)malloc(sizeof(float)*(num_items / 2));
for (int i = 0; i < num_items/2; ++i) {
amp[i] = sqrt( pow(out[i][0],2) + pow(out[i][1], 2));
}`
Here's the python code for it.
import sys
import numpy as np
from scipy.io.wavfile import read
from matplotlib import pyplot as plt
def do_fft(received_wave, Fs=44100):
"""
:param received_wave: wave file data.
:param Fs: Sampling Rate, default = 44100
:return: [Frequency, Amplitude]
"""
# Calculating the fft coeff and amp sqrt(x^2+y^2)
fft_coeff = np.fft.fft(received_wave)
Amp = np.sqrt(np.abs(fft_coeff))
print "FFT_coeff: ",fft_coeff
print "Amp: ",Amp
# calulating size of recieved wave data and creating a freq array based on sampling freq Fs and size
size1=len(received_wave)
freq=np.linspace(0,Fs,size1)
print "Length of recieved wave: ",size1;
# Taking only half sample based on Nyquist-Shannon sampling theorem for ampiltude and frequency
# https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem
Amplitude = Amp[0:int(size1/2)]
Frequency = freq[0:int(size1/2)]
print "\nAmplitude : ", Amplitude
print "\nFreq : ", Frequency
# This shorts the index of the array in acending order
idx = np.argsort(Amplitude)
# freq1 is the maximum freq freq2 second maximum and so on
freq1 = ((idx[-1]) / float(size1)) * Fs
freq2 = ((idx[-2]) / float(size1)) * Fs
freq3 = ((idx[-3]) / float(size1)) * Fs
return Amplitude, Frequency, freq1, freq2, freq3
def read_from_file(file_location):
"""
Read file ad return audio data
:param file_location: location of file.
:return: audio data
"""
data = read(file_location)
# as scipy read function return two array [sample_rate_of_file, [audio_chunks]]
sample_rate, audio_data = data
print "Data: " ,data
for i in range(len(audio_data)):
# print audio_data[i]
pass
print i
return sample_rate, audio_data
def plot_fft(audio_file):
# read audio chunks from audio file
sample_rate, audio_data = read_from_file(audio_file)
# call do_fft() function to get fft ( frequency and amplitude)
Amplitude, Frequency, freq1, freq2, freq3 = do_fft(received_wave=audio_data, Fs=sample_rate)
# plot fft
plt.title("FFT heigest : {}, second_heigest : {}".format(freq1, freq2))
plt.plot(Frequency, Amplitude)
plt.show()
plt.close()
return True
if __name__ == '__main__':
file = "hellotrill.wav"
plot_fft(file)
I'm not understanding as the arrays I'm getting in Python and C++ completely differ.