-
Notifications
You must be signed in to change notification settings - Fork 0
/
testAudio.py
196 lines (140 loc) · 6 KB
/
testAudio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
import math
from helpers import recordAudioToDataVector, dataVectorToWavFile,\
wavFileToDataVector, SVDrankKApproximation, findSpeechPause, computeSVD
## - get data vector of maedee_voice.wav and white_noise.wav, add them together,
## and create .wav file from it
##
currentDirectory = os.path.dirname(__file__)
fileToImport = "new_maedee_voice.wav"
fileLocation = os.path.join(currentDirectory, "Audio/", fileToImport)
(maedeeSampleRate, maedataVector) = wavFileToDataVector(fileLocation)
fileToImport = "new_white_noise.wav"
fileLocation = os.path.join(currentDirectory, "Audio/", fileToImport)
(whiteNoiseSampleRate, whiteNoiseDataVector) = wavFileToDataVector(fileLocation)
fileToImport = "new_combined.wav"
fileLocation = os.path.join(currentDirectory, "Audio/", fileToImport)
(combinedSampleRate, combinedDataVector) = wavFileToDataVector(fileLocation)
#print("Combined sample rate:", str(combinedSampleRate))
#print("Size of combined data vector:", str(combinedDataVector.shape))
#print(combinedDataVector)
speechPauseVector = findSpeechPause(combinedDataVector, combinedSampleRate,\
0.03)
whiteNoiseVariance = np.var(speechPauseVector)
eta = np.sqrt(whiteNoiseVariance)
print("eta =", eta)
# combinedDataVector = maedataVector + whiteNoiseDataVector
# dataVectorToWavFile(combinedDataVector, maedeeSampleRate, "combined")
## calculations to split combined data vector into "windows", each with a
## duration of 30 ms
##
duration = 3 # [s]
# (maedeeSampleRate [samples] / 1 [s]) * (duration [s])
totalSamples = maedeeSampleRate * duration # [samples]
# (duration [s]) / ( (30 [ms] / 1 [window]) * (1 [s] / 1000 [ms]) ) =
numWindows = math.ceil(duration / 0.03) # [windows]
# (totalSamples [samples]) / numWindows [windows]
samplesPerWindow = math.floor(totalSamples / numWindows) # [samples / window]
# (numWindows [windows]) * (samplesPerWindow [samples] / 1 [window])
numCoveredSamples = numWindows * samplesPerWindow # [samples]
numMissedSamples = totalSamples - numCoveredSamples # [samples]
xSamples = np.array([i for i in range(0, totalSamples)])
print("Total samples:", str(totalSamples))
print("Number of 30 ms windows:", str(numWindows))
print("Number of samples per 30 ms window:", str(samplesPerWindow))
print("Samples covered:", numCoveredSamples)
print("Difference in samples:", numMissedSamples)
# # [0, totalSamples - 1], step: samplesPerWindow
# for i in range(0, totalSamples, samplesPerWindow):
# windowVector = combinedDataVector[i : i + samplesPerWindow]
# #print(windowVector)
windowStartIndex = 0
noiselessDataVector = np.zeros([totalSamples, 1])
print("Size of noiseless vector:", str(noiselessDataVector.shape))
for i in range(0, numWindows):
if i == numWindows - 1:
windowVector = combinedDataVector[windowStartIndex:]
else:
windowVector = combinedDataVector[windowStartIndex : windowStartIndex +\
samplesPerWindow]
print(i, "/", numWindows - 1, end="\r")
#print("Length of window vector:", str(windowVector.shape))
H = sp.linalg.hankel(windowVector)
C = (np.transpose(H) @ H)
psvd, singularvalues, qtsvd, svdduration = computeSVD(C)
kEmp = np.count_nonzero(singularvalues > np.sqrt(H.shape[0]) * eta)
print("Rank of H:", str(np.linalg.matrix_rank(H)))
print("Forced rank of Hhat:", str(kEmp))
k, P, Sigma, QT, Hhat, Pk, SigmaK, QTk, duration =\
SVDrankKApproximation(H, k=kEmp)
phiMLS = np.eye(kEmp) - (H.shape[0] * (eta ** 2) *\
np.linalg.matrix_power(SigmaK, -2))
Hhat = Pk @ phiMLS @ SigmaK @ QTk
#shat = np.transpose(Hhat[0])
n = Hhat.shape[1]
shat = np.zeros(n)
for i in range(0, n):
antidiagonal = np.diag(np.fliplr(Hhat), n-i-1)
# Calculate the mean of the antidiagonal
shat[i] = np.mean(antidiagonal)
# shat = shat.reshape(shat.shape[0], 1)
# s = Hhat[0]
# s = s.reshape(s.shape[0], 1)
shat = shat.reshape(shat.shape[0], 1)
#print("Length of s vector:", str(s.shape))
if i == numWindows - 1:
noiselessDataVector[windowStartIndex:] = shat
else:
noiselessDataVector[windowStartIndex : windowStartIndex +\
samplesPerWindow] = shat
#print("Current length of noiseless vector:", str(noiselessDataVector.shape))
#print(noiselessDataVector)
# print(i)
# print(windowVector.shape)
# print(windowVector)
windowStartIndex += samplesPerWindow
plt.figure("Maedee's Voice")
plt.plot(xSamples, maedataVector)
plt.xlabel("Sample number")
plt.ylabel("Amplitude")
plt.ylim((-1, 1))
plt.figure("White Noise")
plt.plot(xSamples, whiteNoiseDataVector)
plt.xlabel("Sample number")
plt.ylabel("Amplitude")
plt.ylim((-1, 1))
plt.figure("Combined")
plt.plot(xSamples, combinedDataVector)
plt.xlabel("Sample number")
plt.ylabel("Amplitude")
plt.ylim((-1, 1))
plt.figure("After Processing")
plt.plot(xSamples, noiselessDataVector)
plt.xlabel("Sample number")
plt.ylabel("Amplitude")
plt.ylim((-1, 1))
plt.show()
print("Size of noiseless vector:", str(noiselessDataVector.shape))
print(noiselessDataVector)
dataVectorToWavFile(noiselessDataVector, 8000, "noiseless")
## convert .wav file to a representative data vector
##
# currentDirectory = os.path.dirname(__file__)
# fileToImport = "maedee_voice.wav"
# fileLocation = os.path.join(currentDirectory, "Audio/", fileToImport)
# sampleRate, dataVector = wavFileToDataVector(fileLocation)
# print(dataVector)
## debugging
##
# print("Inputted sample rate: ", sampleRate, "Hz")
# audioData = recordAudioToDataVector(sampleRate, duration)
# print("Size of recorded data vector: ", audioData.shape)
# print(audioData)
# dataVectorToWavFile(audioData, sampleRate, "output")
# detectedSampleRate, detectedAudioData = wavFileToDataVector("output.wav")
# print("Detected sample rate: ", detectedSampleRate, "Hz")
# print("Size of detected data vector: ", detectedAudioData.shape)
# print(detectedAudioData)