Skip to content

Commit 875c0ab

Browse files
author
Schuler Henry Martin (BhP/HRL3.2-SH1)
committed
Tested lpc function.
1 parent ca186d0 commit 875c0ab

File tree

5 files changed

+204
-109
lines changed

5 files changed

+204
-109
lines changed
Binary file not shown.

playground/windowing/fileHandler.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from tracemalloc import start
2+
import librosa
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
class FileHandler:
7+
def __init__(self, filepath):
8+
self.y, self.sampling_rate = librosa.load(filepath)
9+
self.total_time = self.y.size / self.sampling_rate
10+
11+
print(self.total_time)
12+
13+
def get_frame(self, frame_time, start_frame):
14+
frame_frames = int(self.sampling_rate * frame_time)
15+
return self.y[start_frame:(start_frame + frame_frames)], frame_frames
16+
17+
def view(self):
18+
plt.plot(np.linspace(0, self.y.size, self.y.size), self.y)
19+
plt.show()
20+
21+
def autocorrelate(self, frame_size):
22+
frame_frames = int(self.sampling_rate * frame_size)
23+
frame_y = self.y[3200:(3200 + frame_frames)]
24+
Fr = np.fft.fft(frame_y)
25+
S = Fr * np.conjugate(Fr)
26+
print(Fr)
27+
28+
print(abs(np.fft.ifft(S))[:10])
29+
print(abs(np.fft.ifft(S)).size)
30+
31+
print(librosa.autocorrelate(frame_y)[:10])
32+
print(librosa.autocorrelate(frame_y).size)
33+
34+
plt.plot(np.linspace(0, frame_frames, frame_frames), frame_y)
35+
plt.show()
36+
plt.plot(np.linspace(0, frame_frames, frame_frames), np.fft.ifft(S))
37+
plt.plot(np.linspace(0, frame_frames, frame_frames), librosa.autocorrelate(frame_y))
38+
plt.show()
39+
return librosa.autocorrelate(frame_y * np.hanning(frame_frames))
40+
41+
def get_lpc(self, frame_time):
42+
frame_y, frame_frames = self.get_frame(frame_time, 3200)
43+
44+
return librosa.lpc(frame_y * np.hanning(frame_frames), order=20)[1:]
Loading
Loading

playground/windowing/main.py

Lines changed: 160 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,168 @@
1-
from pickle import FALSE
21
import numpy as np
3-
import matplotlib.pyplot as plot
2+
import matplotlib.pyplot as plt
43
import librosa
4+
import scipy
5+
6+
from fileHandler import FileHandler
57

68
# sin
79

8-
frequency = 3;
9-
frequency2 = 5;
10-
w = 2 * np.pi * frequency;
11-
w2 = 2 * np.pi * frequency2;
12-
time_interval = 1.2;
13-
samples= 800;
14-
time = np.linspace(0, time_interval, samples);
15-
amplitude = np.sin(w*time);
16-
amplitude += np.sin(w2*time);
17-
18-
plot.plot(time, amplitude);
19-
plot.title("Sine wave");
20-
plot.xlabel("Time");
21-
plot.ylabel("Amplitude");
22-
plot.grid(True, which="both");
23-
plot.axhline(y=0, color="k");
24-
plot.show(block=False);
25-
26-
# window
27-
28-
newAmplitude = amplitude * np.hamming(samples)
29-
30-
plot.plot(time, newAmplitude);
31-
plot.title("Sine wave");
32-
plot.xlabel("Time");
33-
plot.ylabel("Amplitude");
34-
plot.grid(True, which="both");
35-
plot.axhline(y=0, color="k");
36-
plot.show();
37-
38-
plot.plot(time, np.hamming(samples))
39-
plot.show();
40-
41-
# fft
42-
43-
def fft(amp):
44-
fourierTransform = np.fft.fft(amp) / len(amp)
45-
fourierTransform = fourierTransform[range(int(len(amp)/2))]
46-
47-
tpCount = len(amp)
48-
values = np.arange(int(tpCount/2))
49-
timePeriod = tpCount/(samples/time_interval)
50-
frequencies = values/timePeriod
51-
52-
plot.plot(frequencies[:15], abs(fourierTransform)[:15])
53-
plot.show(block=False)
54-
55-
def fft2(amp):
56-
n = int(samples/time_interval)
57-
freqs = np.fft.fftfreq(n)
58-
mask = freqs >= 0
59-
fft_vals = np.fft.fft(amp)
60-
fft_theo = 2.0*np.abs(fft_vals/n)
61-
62-
plot.figure(1)
63-
plot.title("OS")
64-
plot.plot(time, amp, color="xkcd:salmon", label="original")
65-
plot.legend()
66-
67-
plot.figure(2)
68-
plot.plot(freqs[mask]*260, fft_theo[:len(mask)][mask], "ro-", label="true fft values")
69-
plot.title("True FFT values")
70-
plot.show(block=False)
10+
def test1():
11+
12+
frequency = 3;
13+
frequency2 = 5;
14+
w = 2 * np.pi * frequency;
15+
w2 = 2 * np.pi * frequency2;
16+
time_interval = 1.2;
17+
samples= 800;
18+
time = np.linspace(0, time_interval, samples);
19+
amplitude = np.sin(w*time);
20+
amplitude += np.sin(w2*time);
21+
22+
plt.plot(time, amplitude);
23+
plt.title("Sine wave");
24+
plt.xlabel("Time");
25+
plt.ylabel("Amplitude");
26+
plt.grid(True, which="both");
27+
plt.axhline(y=0, color="k");
28+
plt.show(block=False);
29+
30+
# window
31+
32+
newAmplitude = amplitude * np.hamming(samples)
33+
34+
plt.plot(time, newAmplitude);
35+
plt.title("Sine wave");
36+
plt.xlabel("Time");
37+
plt.ylabel("Amplitude");
38+
plt.grid(True, which="both");
39+
plt.axhline(y=0, color="k");
40+
plt.show();
41+
42+
plt.plot(time, np.hamming(samples))
43+
plt.show();
44+
45+
# fft
46+
47+
def fft(amp):
48+
fourierTransform = np.fft.fft(amp) / len(amp)
49+
fourierTransform = fourierTransform[range(int(len(amp)/2))]
50+
51+
tpCount = len(amp)
52+
values = np.arange(int(tpCount/2))
53+
timePeriod = tpCount/(samples/time_interval)
54+
frequencies = values/timePeriod
55+
56+
plt.plot(frequencies[:15], abs(fourierTransform)[:15])
57+
plt.show(block=False)
58+
59+
def fft2(amp):
60+
n = int(samples/time_interval)
61+
freqs = np.fft.fftfreq(n)
62+
mask = freqs >= 0
63+
fft_vals = np.fft.fft(amp)
64+
fft_theo = 2.0*np.abs(fft_vals/n)
65+
66+
plt.figure(1)
67+
plt.title("OS")
68+
plt.plot(time, amp, color="xkcd:salmon", label="original")
69+
plt.legend()
70+
71+
plt.figure(2)
72+
plt.plot(freqs[mask]*260, fft_theo[:len(mask)][mask], "ro-", label="true fft values")
73+
plt.title("True FFT values")
74+
plt.show(block=False)
75+
76+
def fft3(amp):
77+
freqs = np.fft.fftfreq(samples)
78+
mask = freqs >= 0
79+
fft = abs(np.fft.fft(amp))[mask]
80+
print(freqs.shape)
81+
print(amp.shape)
82+
print(freqs[0])
83+
plt.figure(1)
84+
plt.plot(np.linspace(0, freqs.size, freqs.size), freqs)
85+
plt.figure(2)
86+
plt.plot(np.linspace(0, fft.size, fft.size) / time_interval, fft, "o-")
87+
plt.show(block=False)
88+
89+
90+
91+
# fft(amplitude);
92+
# fft(newAmplitude);
93+
# fft2(amplitude);
94+
# fft2(newAmplitude)
95+
# fft3(amplitude);
96+
# fft3(newAmplitude);
97+
# plot.show()
98+
99+
y, sr = librosa.load("C:\\Users\\SCU8BH\\Downloads\\Casio-MT-45-Piano-C4.wav")
100+
y_second, sr_second = librosa.load("C:\\Users\\SCU8BH\\Downloads\\1980s-Casio-Harpsichord-C5.wav")
101+
plt.plot(np.linspace(0, y.size/sr, y.size), y)
102+
plt.show()
103+
104+
time_interval = y.size/sr; # sec
105+
samples = y.size;
106+
# fft3(y);
107+
# fft3(y * np.hanning(samples))
108+
# plot.show()
109+
110+
coefficient = 0.1/time_interval # brings everything to 0.1 sec
111+
112+
y_old = y
113+
114+
samples = int(y.size * coefficient);
115+
y = y[:samples];
116+
time_interval = time_interval * coefficient;
117+
118+
plt.plot(np.linspace(0, samples/sr, samples), y)
119+
plt.show()
120+
121+
# fft3(y)
122+
# fft3(y * np.hanning(samples))
123+
# fft3(y * np.hamming(samples))
124+
# plot.show()
125+
126+
# Tests with lpc TODO: Get the meaning of what I am doing here.
127+
128+
# Add a signal to the given tone
129+
y_n = y_second[samples:(2*samples)] # + np.sin(w * np.pi * 200 * np.linspace(0, time_interval, samples)) + np.cos(w * np.pi * 20 * np.linspace(0, time_interval, samples))
130+
131+
print(librosa.lpc(y, order=2))
132+
a = librosa.lpc(y * np.hanning(samples), order=20)
133+
print(a[1:])
134+
b = np.hstack([[0], -1 * a[1:]])
135+
y_hat = scipy.signal.lfilter(b, [1], (y_n * np.hanning(samples)))
136+
fig, ax = plot.subplots()
137+
ax.plot(y_n * np.hanning(samples))
138+
ax.plot(y_hat, linestyle='--')
139+
ax.legend(['y', 'y_hat'])
140+
ax.set_title('LP Model Forward Prediction')
141+
plt.show()
71142

72-
def fft3(amp):
73-
freqs = np.fft.fftfreq(samples)
74-
mask = freqs >= 0
75-
fft = abs(np.fft.fft(amp))[mask]
76-
print(freqs.shape)
77-
print(amp.shape)
78-
print(freqs[0])
79-
plot.figure(1)
80-
plot.plot(np.linspace(0, freqs.size, freqs.size), freqs)
81-
plot.figure(2)
82-
plot.plot(np.linspace(0, fft.size, fft.size) / time_interval, fft, "o-")
83-
plot.show(block=False)
143+
def test2():
144+
file = FileHandler("C:/Users/SCU8BH/Documents/T3000/Studienarbeit/Data/50_speakers_audio_data/Speaker_0000/Speaker_0000_00000.wav")
145+
# file.view()
146+
print(file.autocorrelate(0.1)[0:20])
147+
y, frames = file.get_frame(0.1, 3200)
148+
plt.plot(np.linspace(0, 30, 30), y[0:30])
149+
# plt.show()
150+
lpc = np.flip(file.get_lpc(0.1))
151+
# print(lpc)
152+
new_y = y[0:20]
153+
# print(np.pad(new_y[-20:], (0,(20-new_y.size)), mode='constant'))
154+
# print(np.flip(new_y))
155+
# print(np.dot(np.pad(new_y[-20:], (0,(20-new_y.size)), mode='constant'), lpc))
84156

157+
while len(new_y) < frames:
158+
# get last 20 values of new_y
159+
new_y = np.append(new_y, np.array([np.dot(new_y[-20:], lpc)]))
160+
161+
plt.plot(np.linspace(0, 30, 30), new_y[0:30])
162+
plt.show()
85163

86-
87-
# fft(amplitude);
88-
# fft(newAmplitude);
89-
# fft2(amplitude);
90-
# fft2(newAmplitude)
91-
fft3(amplitude);
92-
fft3(newAmplitude);
93-
plot.show()
94-
95-
y, sr = librosa.load("C:\\Users\\SCU8BH\\Downloads\\Casio-MT-45-Piano-C4.wav")
96-
plot.plot(np.linspace(0, y.size/sr, y.size), y)
97-
plot.show()
98-
99-
time_interval = y.size/sr; # sec
100-
samples = y.size;
101-
fft3(y);
102-
fft3(y * np.hanning(samples))
103-
plot.show()
104-
105-
coefficient = 0.1/time_interval # brings everything to 0.1 sec
106-
107-
samples = int(y.size * coefficient);
108-
y = y[:samples];
109-
time_interval = time_interval * coefficient;
110-
111-
plot.plot(np.linspace(0, samples/sr, samples), y)
112-
plot.show()
113-
114-
fft3(y)
115-
fft3(y * np.hanning(samples))
116-
fft3(y * np.hamming(samples))
117-
plot.show()
164+
165+
# test2()
166+
167+
print(librosa.lpc(np.array([13.77, 13.6, 13.11, 12.38, 11.48, 10.45]), order=1))
168+
print(librosa.lpc(np.array([13.77, 13.6, 13.11, 12.38, 11.48, 10.45]), order=2))

0 commit comments

Comments
 (0)