def test_zpFFTsizeExpt(self): data = load(5, 1) eMX1_80, eMX2_80, eMX3_80 = data['output'] aMX1_80, aMX2_80, aMX3_80 = zpFFTsizeExpt(data['input']['x'], data['input']['fs']) self.assertTrue(np.allclose(eMX1_80, aMX1_80)) self.assertTrue(np.allclose(eMX2_80, aMX2_80)) self.assertTrue(np.allclose(eMX3_80, aMX3_80))
def test_suppressFreqDFTmodel2(self): data = load(4, 2) eY, eYfilt = data['output'] aY, aYfilt = suppressFreqDFTmodel(data['input']['x'], data['input']['fs'], data['input']['N']) print('eYfilt', eYfilt) print('aYfilt', aYfilt) self.assertTrue(np.allclose(eY, aY)) self.assertTrue(np.allclose(eYfilt, aYfilt))
def test_testRealEven2(self): testData = load(3, 2) eIsRealEven, eDftbuffer, eX = testData['output'] aIsRealEven, aDftbuffer, aX = testRealEven(testData['input']['x']) self.assertEquals(eIsRealEven, aIsRealEven); self.assertTrue(np.allclose(eDftbuffer, aDftbuffer)) self.assertTrue(np.allclose(np.real(eX), np.real(aX))) self.assertTrue(np.allclose(np.imag(eX), np.imag(aX)))
def test_optimalZeropad2(self): testData = load(2, 2) mX = optimalZeropad(testData['input']['x'], testData['input']['fs'], testData['input']['f']) mX[mX < -120] = -120 testData['output'][testData['output'] < -120] = -120 self.assertTrue(np.allclose(testData['output'], mX))
condition. In this case, output mX is 241 samples in length and has non-zero values at bin indices 3 and 8 (corresponding to the frequency values of 300 and 800 Hz, respectively). You can create a test signal x by generating and adding two sinusoids of the given frequencies. """ def minimizeEnergySpreadDFT(x, fs, f1, f2): """ Inputs: x (numpy array) = input signal fs (float) = sampling frequency in Hz f1 (float) = frequency of the first sinusoid component in Hz f2 (float) = frequency of the second sinusoid component in Hz Output: The function should return mX (numpy array) = The positive half of the DFT spectrum (in dB) of the M sample segment of x. mX is (M/2)+1 samples long (M is to be computed) """ ## Your code here a1 = fs / float(f1) a2 = fs / float(f2) m = int(a1 * a2 / gcd(a1, a2)) mx = 20 * np.log10(np.abs(fft(x[:m]))) return mx[:(m/2)+1] if __name__ == '__main__': x = load(1, 2) #print x['output'][:20] print minimizeEnergySpreadDFT(**x['input']) print x['output']
frequency band. ODF[:,0]: ODF computed in band 0 < f < 3000 Hz ODF[:,1]: ODF computed in band 3000 < f < 10000 Hz """ ### your code here engEnv = computeEngEnv(inputFile, window, M, N, H) res = [] for i in range(0, len(engEnv)): if i == 0: res.append([engEnv[i][0], engEnv[i][1]]) else: res.append([ engEnv[i][0] - engEnv[i - 1][0], engEnv[i][1] - engEnv[i - 1][1] ]) res = np.array(res) res[res < 0] = 0 plt.plot(res[:, 0]) plt.plot(res[:, 1]) plt.show() return res if __name__ == '__main__': from loadTestCases import load a = load(4) print a['input'] print a['output'] print computeODF(**a['input'])
def get_test_case(part_id, case_id): import loadTestCases testcase = loadTestCases.load(part_id, case_id) return testcase
import matplotlib.pyplot as plt import numpy as np # import test_sinusoids import A5Part1, A5Part4 from loadTestCases import load #fs = 1000 #f = 100 #A0 = 10 #M = 25 testCase = load(4, 1) result = A5Part4.selectFlatPhasePeak(**testCase['input']) print result print testCase['input'] print np.std(testCase['input']['pX']) print testCase['output'] #x = A0 * np.cos(2 * np.pi * f * np.arange(M) / fs) #x = np.array([1, 2, 3, 4, 1, 2, 3]) #(isRealEven, dftbuffer, X) = A3Part3.testRealEven(x) #print (isRealEven, dftbuffer, X)
maximum value at bin index 6 corresponding to the frequency of 250 Hz. The output mX you return is 121 samples in length. """ def optimalZeropad(x, fs, f): """ Inputs: x (numpy array) = input signal of length M fs (float) = sampling frequency in Hz f (float) = frequency of the sinusoid in Hz Output: The function should return mX (numpy array) = The positive half of the DFT spectrum of the N point DFT after zero-padding x appropriately (zero-padding length to be computed). mX is (N/2)+1 samples long """ ## Your code here periodnum = fs / f zeropad = int(((len(x) / int(periodnum)) + 1) * periodnum - len(x)) x = np.append(x, np.zeros(zeropad)) mx = 20 * np.log10(np.abs(fft(x))) return mx[:len(x) / 2 + 1] if __name__ == '__main__': x = load(2, 1) #print x['output'][:20] print optimalZeropad(**x['input']) print x['output']
def compareAnswers(t, a): for i in range(t.size): if (abs(t[i]-a[i])>1.0e-06): print("Error", i,abs(t[i]-a[i])) return False return True #tc = ltc.load(1, 1) #tc = ltc.load(1, 2) #print("TestCase:",tc) #mx = A3Part1.minimizeEnergySpreadDFT(tc['input']['x'], tc['input']['fs'], tc['input']['f1'], tc['input']['f2']) #print(mx) #compareAnswersDB(tc['output'],mx) tc = ltc.load(2, 1) tc = ltc.load(2, 2) print("TestCase:",tc) mx = A3Part2.optimalZeropad(tc['input']['x'], tc['input']['fs'], tc['input']['f']) print(mx) print("Compare",compareAnswersDB(tc['output'],mx)) #tc = ltc.load(3, 1) #tc = ltc.load(3, 2) #print("TestCase:",tc) #(isRealEven, dftbuffer, X) = A3Part3.testRealEven(tc['input']['x']) #print("isRealEven:",isRealEven) #print("dftBuffer:",dftbuffer) #print("X",X) #tc = ltc.load(4, 1)
engEnv[:,0]: Energy envelope in band 0 < f < 3000 Hz (in dB) engEnv[:,1]: Energy envelope in band 3000 < f < 10000 Hz (in dB) """ ### your code here fs, x = UF.wavread(inputFile) w = get_window(window, M) mx, px = stft.stftAnal(x, w, N, H) mx = 10**(mx / 20.) res = [] for i in range(0, len(mx)): low = 0. high = 0. for j in range(1, len(mx[i])): rate = fs * j / (N + 0.0) if rate < 3000: low += mx[i][j]**2 elif rate < 10000: #print j high += mx[i][j]**2 res.append([10 * np.log10(low), 10 * np.log10(high)]) return np.array(res) if __name__ == '__main__': from loadTestCases import load a = load(3) print a['input'] print a['output'] print computeEngEnv(**a['input'])
Output: The function should return a numpy array containing the main lobe of the magnitude spectrum of the window in decibels (dB). """ w = get_window(window, M) # get the window ### Your code here fftx = 20 * np.log10(np.abs(fftshift(fft(w, 8 * M))) + eps) left = [] idx = 4 * M for i in range(idx - 1, 0, -1): left.append(fftx[i]) if fftx[i] < fftx[i-1]: break right = [] for i in range(idx, len(fftx) - 1): right.append(fftx[i]) if fftx[i] < fftx[i+1]: break return np.array(left[::-1] + right) if __name__ == '__main__': from loadTestCases import load a = load(1) print a['input'] print a['output'] print extractMainLobe(**a['input'])
fs (float) = sampling frequency in Hz f1 (float) = frequency of the first sinusoid component in Hz f2 (float) = frequency of the second sinusoid component in Hz Output: The function should return mX (numpy array) = The positive half of the DFT spectrum (in dB) of the M sample segment of x. mX is (M/2)+1 samples long (M is to be computed) """ T1 = 1./f1 T2 = 1./f2 Ts1 = int(T1*fs) Ts2 = int(T2*fs) M = int(Ts1*Ts2/gcd(Ts1,Ts2)) X = fft(x,M) mX = 20*np.log10(abs(X[:int(M/2)+1])) return mX if __name__ == "__main__": cases = [1,2] for i in cases: testcase = loadTestCases.load(partId=1, caseId=i) ground_truth = testcase['output'] my_output = minimizeEnergySpreadDFT(**testcase['input']) dB_cutoff = -120. if np.allclose(my_output[my_output>dB_cutoff], ground_truth[ground_truth>dB_cutoff]): print('Test passed') else: print('my output:{0}'.format(my_output)) print('ground truth: {0}'.format(ground_truth))
def test_minimizeEnergySpreadDFT2(self): testData = load(1, 2) mX = minimizeEnergySpreadDFT(testData['input']['x'], testData['input']['fs'], testData['input']['f1'], testData['input']['f2']) self.assertTrue(np.allclose(testData['output'], mX))
bin_freqs = np.arange(N) / T low_cutoff = 3000 high_cutoff = 10000 k1 = np.argmin(bin_freqs < low_cutoff) k2 = np.argmax(bin_freqs > low_cutoff) k3 = np.argmin(bin_freqs < high_cutoff) return np.vstack((bandE(mX_lin, 1, k1), bandE(mX_lin, k2, k3))).T if __name__ == '__main__': import loadTestCases fun = computeEngEnv nr_parts = 3 question_nr = 3 for ii in range(1, nr_parts + 1): print '=' * 30 print 'Part {}'.format(ii) print '=' * 30 print 'MINE' mine = fun(**loadTestCases.load(question_nr, ii)['input']) print mine print '-' * 20 print 'THEIRS' theirs = loadTestCases.load(question_nr, ii)['output'] print theirs print '-' * 20 print 'EQUAL?' print np.allclose(mine, theirs) print '-' * 20 print '\n'
The function should return a tuple (isRealEven, dftbuffer, X) isRealEven (boolean) = True if the input x is real and even, and False otherwise dftbuffer (numpy array, possibly complex) = The M point zero phase windowed version of x X (numpy array, possibly complex) = The M point DFT of dftbuffer """ ## Your code here idx = len(x) / 2 trans = np.concatenate((x[idx:], x[:idx])).astype(float) #print trans dft = fft(trans) isrealeven = True for i in range(0, idx): if np.imag(x[i]) != 0 or x[i] != x[-(i + 1)]: isrealeven = False break for d in dft: if abs(np.imag(d)) != 0: isrealeven = False break for i in range(1, len(dft)): if abs(np.real(dft[i]) - np.real(dft[-i])) > 0.000000000001: isrealeven = False return (isrealeven, trans, dft) if __name__ == '__main__': from loadTestCases import load x = load(3, 2) print x['output'] print testRealEven(**x['input'])
import matplotlib.pyplot as plt import numpy as np # import test_sinusoids import A6Part3 from loadTestCases import load testCase = load(3, 2) print testCase['input'] print testCase['output'] result = A6Part3.estimateInharmonicity(**testCase['input']) print result
dftv = dftAnal(x, w, N) #print dftv[0].shape filterDft = dftv[0].copy() for i in range(len(filterDft)): fz = fs * i / N filterDft[i] = -120 if fz > 70: break #print str(fz) + ' ' + str(i) return (dftSynth(dftv[0], dftv[1], M) * outputScaleFactor, dftSynth(filterDft, dftv[1], M) * outputScaleFactor) if __name__ == '__main__': from loadTestCases import load x = load(4, 1) from matplotlib import pyplot as plt #plt.plot( x['output'][0]) #plt.plot( x['output'][1]) #plt.show() tmpout = suppressFreqDFTmodel(**x['input']) M = len(x['output'][1]) #print dftAnal(x['output'][1], get_window('boxcar', M), M)[0] #print dftAnal(tmpout[1], get_window('boxcar', M), M)[0] print x['output'][0][:10] print tmpout[0][:10] #plt.plot( tmpout[0]) #plt.plot( tmpout[1]) #plt.show()