1+ from pickle import FALSE
2+ import numpy as np
3+ import matplotlib .pyplot as plot
4+
5+ # sin
6+
7+ frequency = 30 ;
8+ frequency2 = 5 ;
9+ w = 2 * np .pi * frequency ;
10+ w2 = 2 * np .pi * frequency2 ;
11+ time_interval = 1.8 ;
12+ samples = 800 ;
13+ time = np .linspace (0 , time_interval , samples );
14+ amplitude = np .sin (w * time );
15+ amplitude += np .sin (w2 * time );
16+
17+ plot .plot (time , amplitude );
18+ plot .title ("Sine wave" );
19+ plot .xlabel ("Time" );
20+ plot .ylabel ("Amplitude" );
21+ plot .grid (True , which = "both" );
22+ plot .axhline (y = 0 , color = "k" );
23+ plot .show (block = False );
24+
25+ # window
26+
27+ newAmplitude = amplitude * np .hamming (samples )
28+
29+ plot .plot (time , newAmplitude );
30+ plot .title ("Sine wave" );
31+ plot .xlabel ("Time" );
32+ plot .ylabel ("Amplitude" );
33+ plot .grid (True , which = "both" );
34+ plot .axhline (y = 0 , color = "k" );
35+ plot .show ();
36+
37+ plot .plot (time , np .hamming (samples ))
38+ plot .show ();
39+
40+ # fft
41+
42+ def fft (amp ):
43+ fourierTransform = np .fft .fft (amp ) / len (amp )
44+ fourierTransform = fourierTransform [range (int (len (amp )/ 2 ))]
45+
46+ tpCount = len (amp )
47+ values = np .arange (int (tpCount / 2 ))
48+ timePeriod = tpCount / (samples / time_interval )
49+ frequencies = values / timePeriod
50+
51+ plot .plot (frequencies [:15 ], abs (fourierTransform )[:15 ])
52+ plot .show (block = False )
53+
54+ def fft2 (amp ):
55+ n = int (samples / time_interval )
56+ freqs = np .fft .fftfreq (n )
57+ mask = freqs > 0
58+ fft_vals = np .fft .fft (amp )
59+ fft_theo = 2.0 * np .abs (fft_vals / n )
60+
61+ plot .figure (1 )
62+ plot .title ("OS" )
63+ plot .plot (time , amp , color = "xkcd:salmon" , label = "original" )
64+ plot .legend ()
65+
66+ plot .figure (2 )
67+ plot .plot (freqs [mask ]* 260 , fft_theo [:len (mask )][mask ], "ro-" , label = "true fft values" )
68+ plot .title ("True FFT values" )
69+ plot .show (block = False )
70+
71+
72+ # fft(amplitude);
73+ # fft(newAmplitude);
74+ fft2 (amplitude );
75+ fft2 (newAmplitude )
76+ plot .show ()
0 commit comments