Skip to content

Commit ca186d0

Browse files
Schuler Henry Martin (BhP/HRL3.2-SH1)Schuler Henry Martin (BhP/HRL3.2-SH1)
authored andcommitted
Added import from wav + new fft function (own knowledge).
1 parent 28ca7b5 commit ca186d0

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

playground/windowing/main.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from pickle import FALSE
22
import numpy as np
33
import matplotlib.pyplot as plot
4+
import librosa
45

56
# sin
67

7-
frequency = 30;
8+
frequency = 3;
89
frequency2 = 5;
910
w = 2 * np.pi * frequency;
1011
w2 = 2 * np.pi * frequency2;
11-
time_interval = 1.8;
12+
time_interval = 1.2;
1213
samples= 800;
1314
time = np.linspace(0, time_interval, samples);
1415
amplitude = np.sin(w*time);
@@ -54,7 +55,7 @@ def fft(amp):
5455
def fft2(amp):
5556
n = int(samples/time_interval)
5657
freqs = np.fft.fftfreq(n)
57-
mask = freqs > 0
58+
mask = freqs >= 0
5859
fft_vals = np.fft.fft(amp)
5960
fft_theo = 2.0*np.abs(fft_vals/n)
6061

@@ -68,9 +69,49 @@ def fft2(amp):
6869
plot.title("True FFT values")
6970
plot.show(block=False)
7071

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)
84+
85+
7186

7287
# fft(amplitude);
7388
# fft(newAmplitude);
74-
fft2(amplitude);
75-
fft2(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))
76117
plot.show()

0 commit comments

Comments
 (0)