def smoothing(wav_data): r = 3 wav = zeros(len(wav_data)) for i in range(r, len(wav) - r): wav[i] = sum(wav_data[i - r:i + r + 1]) / (2 * r + 1) return wav
def test_rand(self): import random def zeros_sol(n): x = n // 5 return x + zeros_sol(x) if x else 0 for _ in range(100): n = random.randint(0, 1000000000) self.assertEqual(zeros(n), zeros_sol(n), "Testing with n = %d" % n)
def test(self): self.assertEqual(zeros(0), 0, "Testing with n = 0") self.assertEqual(zeros(6), 1, "Testing with n = 6") self.assertEqual(zeros(30), 7, "Testing with n = 30") self.assertEqual(zeros(100), 24, "Testing with n = 100") self.assertEqual(zeros(1000), 249, "Testing with n = 1000") self.assertEqual(zeros(100000), 24999, "Testing with n = 100000") self.assertEqual(zeros(1000000000), 249999998, "Testing with n = 1000000000")
def shiftallpeaks(peaks1, peaks2, wav_data3): """ Shift all peaks paramaters(peaks1,peaks2,wav_data3) """ n = len(peaks1) x3 = transform(wav_data3,False) new = zeros(len(x3) // 2) new = new.astype(dtype=np.complex) Peaks1 = zeros(n + 2) Peaks1[1:n + 1] = peaks1 Peaks1[n + 1] = len(x3) // 2 Peaks1 = Peaks1.astype(np.int16) Peaks2 = zeros(n + 2) Peaks2[1:n + 1] = peaks2 Peaks2[n + 1] = len(x3) // 2 Peaks2 = Peaks2.astype(np.int16) for j in range(0, n + 1): k = 0 for i in range(Peaks2[j], Peaks2[j + 1]): index = Peaks1[j] + np.floor(scale[j + 1] * k) k += 1 index = int(index) new[i] = x3[index] # run this if you wish to replace duplicates with zeros for j in range(len(new) - 1): if new[j] == new[j + 1]: new[j] = 0.0 return new
# input in waves here FILENAME1 = "source.wav" FILENAME2 = "map.wav" FILENAME3 = "target.wav" rate1, wav_data1 = wavfile.read(FILENAME1) rate2, wav_data2 = wavfile.read(FILENAME2) rate3, wav_data3 = wavfile.read(FILENAME3) # coeficients of regression coef1 = regressionCoefficients(wav_data1, 0.01) coef2 = regressionCoefficients(wav_data2, 0.3) # include end points of regression n = len(coef1) C1 = zeros(n + 2) C1[1:n + 1] = coef1 C1[n + 1] = 1.2 * C1[n] C2 = zeros(n + 2) C2[1:n + 1] = coef2 C2[n + 1] = 1.2 * C2[n] # plot coefficients plt.plot(C1, C2, 'o') plt.show() # Construct polynomial of order n-1 X = np.asmatrix(C1) Y = np.asmatrix(C2) b = np.transpose(Y) A = zeros([n, n])
f2 = linspace(0, 8000, L2) # x2 = np.fft.fft(wav2) x2 = transform(wav2,False) X2 = np.abs(x2) / L2 # magnitude of coefficient of FFT of target voice to be replicated ratio = abs(x2) / abs(x1) # ratio of coefficients r=d/c Freq = linspace(0, 8000, L1) plt.plot(Freq, ratio) # plot ratio over double sided FFT spectrum # This will help us cut down on the ratio . # You need to pick values of h>1. # The closer to 1 , the more it shrinks # The farther h is from 1 , a the smaller the reduction h = 1.25 Ratio = zeros(len(ratio)) for i in range(len(ratio)): Ratio[i] = ((h - 1) * ratio[i] + 1) / h plt.plot(Freq , Ratio) # plt.show() # Apply ratio multipilcation on third audio sample L3 = len(wav3) f3 = linspace(0, 8000, L3) x3 = transform(wav3,False) x3 = x3 * Ratio ifftplot=transform(x3,True) # ifftplot=smoothing(ifftplot) filteredwrite=np.round(ifftplot).astype('int16')