Reputation: 53
i can get magnitude of signal coming from .wav file , but how to get the phase of that signal too ,,, Here is the where i browse for .wav file and extract the signal
def browse_wav(self):
filepath = QtGui.QFileDialog.getOpenFileName(self, 'Single File', "C:\Users\Hanna Nabil\Documents",'*.wav')
f= str(filepath)
if f != "":
spf = wave.open(f, 'r')
import contextlib
with contextlib.closing(wave.open(f, 'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = frames / float(rate)
print "Duration is " , duration
# Extract Raw Audio from Wav File
self.signal = spf.readframes(-1)
self.signal = np.fromstring(self.signal, 'Int16')
self.fs = spf.getframerate()
print "Sampling Rate is " ,self.fs
# If Stereo
if spf.getnchannels() == 2:
print 'Just mono files'
sys.exit(0)
#self.time = np.linspace(0, len(self.signal) / fs, num=len(self.signal))
self.time = np.linspace(0, duration, self.fs * duration)
self.xfourier = fftfreq(self.signal.size, d=self.time[1] - self.time[0])
self.yfourier = np.abs(fft(self.signal)) # signal magnitude
self.zico = self.yfourier
self.cut_signal = ifft(self.zico)
Upvotes: 5
Views: 22109
Reputation: 29
For the answer from Lukas Lalinsky I find I have to add pi/2 for some reason to get the correct answer.
Also for points:
N = len(t)
Amplitude = 2/N * np.abs(magnitude)
Upvotes: -1
Reputation: 41306
The complex spectrum contains both the magnitude and phase. You get the magnitude by calculating the absolute value and you get the phase by calculating the angle. You can use numpy.angle()
to get the phase:
spectrum = fft(self.signal)
magnitude = np.abs(spectrum)
phase = np.angle(spectrum)
Upvotes: 12