I want to use python to see what the audio response of a speaker is. This would be done by comparing the output of the speaker measured with a microphone to the given input. That means that for both of these signals that will be in the time domain I need to perform a FFT. Eventually I want to play a target curve close to a Harman curve. I tried to generate the signal to put it in a wav file and when checking if the signal does what I ask with the FFT I got some weird behaviour. So then I tried to perform an FFT on a sine wave that has a constant amplitude and goes from 21-19700 Hz in 5 seconds with a sample rate of 44100 Hz. I would like to use the welch method where I took the window to be the full length of the sample as in this case the signal I generated does not have any noise. This got me the following result:
I used the following python code
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import welch
samplerate = 44100
duration = 5
time = np.linspace(0,duration,samplerate*duration, endpoint= False)
frequency = np.linspace(21,19700,len(time),endpoint= False)
Const_wave = np.sin(np.pi*2*frequency*time)
f, check = welch(Const_wave,samplerate,nperseg=len(Wave))
plt.plot(f,check)
plt.xscale('log')
plt.xlabel('Frequency [Hz]')
plt.yscale('log')
plt.ylabel('Power')
plt.grid()
plt.show()
Afterwards, I tried the scipy.fft module which yielded a different but also not expected result:
for this I used the following code:
f = rfftfreq(samplerate*duration,1/samplerate)
check = rfft(Const_wave)
plt.plot(f,check)
plt.xscale('log')
plt.xlabel('Frequency [Hz]')
plt.yscale('log')
plt.ylabel('Power')
plt.grid()
plt.show()
I would expect to see a flat plateau of equal power on all frequencies, but that is not happening. I also tried to do the same with a linear sweep wav file generated by REW and got similar results, so is my expectation of a flat plateau incorrect or is there something else at play here?