11from pickle import FALSE
22import numpy as np
33import matplotlib .pyplot as plot
4+ import librosa
45
56# sin
67
7- frequency = 30 ;
8+ frequency = 3 ;
89frequency2 = 5 ;
910w = 2 * np .pi * frequency ;
1011w2 = 2 * np .pi * frequency2 ;
11- time_interval = 1.8 ;
12+ time_interval = 1.2 ;
1213samples = 800 ;
1314time = np .linspace (0 , time_interval , samples );
1415amplitude = np .sin (w * time );
@@ -54,7 +55,7 @@ def fft(amp):
5455def 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 ))
76117plot .show ()
0 commit comments