The objective of my project is to synthesize the sound. What I want to do is to read a wave file and convert it to the amplitude spectrum. Since I am interested in the Magnitude and corresponding Frequencies. I also need to Change the Magnitude of certain frequencies(which i get) so that i can generate different sounds wav file back and play it. However, even without altering the magnitude the reconstructed signal is full of noise.
In simple words , read file --- FFT--- vary magnitude --- play it.
below is the Code
import scipy.io.wavfile
import sounfile as sf
data , rate = sf.read("bird_chirp.wav")
FFt_data =np.fft.rfft(data)
magnitude = np.abs(FFt_data)
phase = np.angle(FFt_data)
N= len(data) # Define the length of the wav file
timestamp = np.linspace(0.0, N*T, N)
T= 1.0/rate
n = data.size
#get the corresponding Frequencies
freq = np.fft.rfftfreq(n, d=1./rate)
# save it as a Dataframe
df = {"freq":freq, "mag":magnitude}
df =pd.DataFrame(df)
#Normalize the magnitude
a=df["mag"]
norm_a = a/a.max(axis=0)
df["mag"] = norm_a
# here I would play around with magnitude , make it high or low
#code to change magnitude
#Get back the new data to write in wav
y=0
for magnitudes ,frequencies in df.iterrows():
y+= magnitudes["mag"]*(np.sin(frequencies["freq"] *2.0*np.pi*timestamp))
#save it
sf.write(file="test.wav", data=y,samplerate=rate)
The code plays sound full of noise.