File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ import numpy as np
3+ import matplotlib .pyplot as plt
4+ from pathlib import Path
5+
6+ ROOT = Path (__file__ ).resolve ().parents [1 ]
7+ OUT = ROOT / "docs" / "assets" / "lab05_resampling_polyphase.png"
8+ OUT .parent .mkdir (parents = True , exist_ok = True )
9+
10+ fs = 8000
11+ N = 1024
12+ t = np .arange (N )/ fs
13+
14+ # input signal
15+ x = np .sin (2 * np .pi * 700 * t ) + 0.5 * np .sin (2 * np .pi * 1800 * t )
16+
17+ L = 3
18+ M = 2
19+
20+ # naive upsample
21+ xu = np .zeros (len (x )* L )
22+ xu [::L ] = x
23+
24+ # simple FIR (low-pass)
25+ num_taps = 63
26+ n = np .arange (num_taps ) - (num_taps - 1 )/ 2
27+ h = np .sinc (n / L ) * np .hanning (num_taps )
28+ h /= np .sum (h )
29+
30+ xf = np .convolve (xu , h , mode = 'same' )
31+
32+ # decimate
33+ y = xf [::M ]
34+
35+ plt .figure (figsize = (10 ,6 ))
36+
37+ plt .subplot (3 ,1 ,1 )
38+ plt .plot (x [:200 ])
39+ plt .title ("Input signal" )
40+
41+ plt .subplot (3 ,1 ,2 )
42+ plt .plot (xu [:400 ])
43+ plt .title ("Upsampled (zero insertion)" )
44+
45+ plt .subplot (3 ,1 ,3 )
46+ plt .plot (y [:200 ])
47+ plt .title ("Resampled output (L/M)" )
48+
49+ plt .tight_layout ()
50+ plt .savefig (OUT , dpi = 150 )
51+ print ("Saved:" , OUT )
You can’t perform that action at this time.
0 commit comments