I am trying to plot the fft of a wav file, I have successfully completed it using the regular fft but I wanted to experiment with rfft
as my application was to perform this in music. When I try to plot xf
and yf
(figure 2) I run into an issue where xf
is half the length of yf
and I can't figure out why, I assume its due to the negative frequencies missing but I thought changing both function calls to rfft
and rfftfreq
would handle it.
import numpy as np
import soundfile as sf
import matplotlib.pyplot as plt
square = 'square.wav'
sine = 'sine.wav'
k = '1Khz.wav'
cello = 'cello.wav'
data, fs = sf.read(k)
#Plot the Signal
N = len(data)
T = 1.0/fs
x = np.linspace(0, (N*T), N)
plt.plot(x, data)
plt.grid()
count = 0
yf = np.fft.rfft(data)
xf = np.fft.rfftfreq(yf.size, d=T)
plt.figure(2)
plt.plot(xf, yf)
plt.show()