Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def read_samplepoints(file_name):
- sampFreq, snd1 = wavfile.read(file_name)
- samp_points = len(snd1)
- data_type = snd1.dtype
- return samp_points, data_type
- def maximum_amplitude(file_name):
- sampFreq, snd = wavfile.read(file_name)
- snd = snd / (2.**15)
- s1 = snd[:,0]
- s2 = snd[:,1]
- max_s1 = np.max(s1)
- max_s2 = np.max(s2)
- return max_s1, max_s2
- def plot_signal(file_name):
- sampFreq, snd = wavfile.read(file_name)
- snd = snd / (2.**15)
- s1 = snd[:,0]
- s2 = snd[:,1]
- timeArray = arange(0, len(snd), 1)
- timeArray = timeArray / sampFreq
- timeArray = timeArray * 1000 #scale to milliseconds
- plt.plot(timeArray, s1, color='k')
- plt.ylabel('LeftChannel_Amplitude')
- plt.xlabel('Time (ms)')
- plt.show()
- timeArray2 = arange(0, len(snd), 1)
- timeArray2 = timeArray2 / sampFreq
- timeArray2 = timeArray2 * 1000 #scale to milliseconds
- plt.plot(timeArray2, s2, color='k')
- plt.ylabel('RightChannel_Amplitude')
- plt.xlabel('Time (ms)')
- plt.show()
- n = len(s1)
- p = fft(s1) # take the fourier transform
- m = len(s2)
- p2 = fft(s2)
- nUniquePts = ceil((n+1)/2.0)
- p = p[0:nUniquePts]
- p = abs(p)
- mUniquePts = ceil((m+1)/2.0)
- p2 = p2[0:mUniquePts]
- p2 = abs(p2)
- '''
- Left Channel
- '''
- p = p / float(n) # scale by the number of points so that
- # the magnitude does not depend on the length
- # of the signal or on its sampling frequency
- p = p**2 # square it to get the power
- # multiply by two (see technical document for details)
- # odd nfft excludes Nyquist point
- if n % 2 > 0: # we've got odd number of points fft
- p[1:len(p)] = p[1:len(p)] * 2
- else:
- p[1:len(p) -1] = p[1:len(p) - 1] * 2 # we've got even number of points fft
- freqArray = arange(0, nUniquePts, 1.0) * (sampFreq / n);
- plt.plot(freqArray/1000, 10*log10(p), color='k')
- plt.xlabel('LeftChannel_Frequency (kHz)')
- plt.ylabel('LeftChannel_Power (dB)')
- plt.show()
- '''
- Right Channel
- '''
- p2 = p2 / float(m) # scale by the number of points so that
- # the magnitude does not depend on the length
- # of the signal or on its sampling frequency
- p2 = p2**2 # square it to get the power
- # multiply by two (see technical document for details)
- # odd nfft excludes Nyquist point
- if m % 2 > 0: # we've got odd number of points fft
- p2[1:len(p2)] = p2[1:len(p2)] * 2
- else:
- p2[1:len(p2) -1] = p2[1:len(p2) - 1] * 2 # we've got even number of points fft
- freqArray2 = arange(0, mUniquePts, 1.0) * (sampFreq / m);
- plt.plot(freqArray2/1000, 10*log10(p2), color='k')
- plt.xlabel('RightChannel_Frequency (kHz)')
- plt.ylabel('RightChannel_Power (dB)')
- plt.show()
- max_p = np.max(10*log10(p))
- max_p2 = np.max(10*log10(p2))
- return max_p, max_p2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement