-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdownsample.m
More file actions
128 lines (104 loc) · 4.19 KB
/
Copy pathupdownsample.m
File metadata and controls
128 lines (104 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
clc; clear;
Fs1 = 100;
n = 0:1/Fs1:100-(1/Fs1);
N = length(n);
% Original signals
sin_wave = sin(5*n*2*pi); % 5 Hz sine
random = 2*round(rand(1,N))-1; % Random ±1
% Lowpass filter (bandwidth limit to 0.1 * Fs)
coeffs = firls(30, [0 0.2 0.22 1], [1 1 0 0]);
sin_bwlimited = filter(coeffs, 1, sin_wave);
random_bwlimited = filter(coeffs, 1, random);
% Normalized frequency axis
f_norm = (-N/2:N/2-1) / N;
%% Decimation by M=5
M = 5;
sin_decimated = sin_bwlimited(1:M:end);
random_decimated = random_bwlimited(1:M:end);
N_dec = length(sin_decimated);
f_norm_dec = (-N_dec/2:N_dec/2-1) / N_dec;
%% Upsampling (Interpolation) by L=5
L = 5;
% Zero-stuffing
sin_upsampled = zeros(1, N_dec * L);
sin_upsampled(1:L:end) = sin_decimated;
random_upsampled = zeros(1, N_dec * L);
random_upsampled(1:L:end) = random_decimated;
% Interpolation filter (lowpass to remove images, gain = L)
coeffs_interp = L * firls(30, [0 0.2/L 0.22/L 1], [1 1 0 0]);
sin_interpolated = filter(coeffs_interp, 1, sin_upsampled);
random_interpolated = filter(coeffs_interp, 1, random_upsampled);
N_up = length(sin_interpolated);
f_norm_up = (-N_up/2:N_up/2-1) / N_up;
%% Figure
figure('Position', [100 100 1200 800]);
% Row 1: Time domain - Original and Filtered
subplot(3,4,1); % (a) Original sine
stem(sin_wave, 'o', 'MarkerSize', 2);
xlim([200 250]); ylim([-1.5 1.5]); grid;
xlabel('Discrete time (n)'); ylabel('Amplitude');
title('(a) Original Sine');
subplot(3,4,2); % (b) Original random
stem(random, 'o', 'MarkerSize', 2);
xlim([200 250]); ylim([-1.5 1.5]); grid;
xlabel('Discrete time (n)'); ylabel('Amplitude');
title('(b) Original Random');
subplot(3,4,3); % (c) Filtered sine
stem(sin_bwlimited, 'o', 'MarkerSize', 2);
xlim([200 250]); ylim([-1.5 1.5]); grid;
xlabel('Discrete time (n)'); ylabel('Amplitude');
title('(c) Filtered Sine');
subplot(3,4,4); % (d) Filtered random
stem(random_bwlimited, 'o', 'MarkerSize', 2);
xlim([200 250]); ylim([-1.5 1.5]); grid;
xlabel('Discrete time (n)'); ylabel('Amplitude');
title('(d) Filtered Random');
% Row 2: FFT of filtered and decimated
subplot(3,4,5); % (e) FFT of filtered sine
sin_fft = 20*log10(abs(fftshift(fft(sin_bwlimited))));
plot(f_norm, sin_fft, 'k');
xlim([-0.5 0.5]); ylim([-200 0]); grid;
xlabel('Frequency f_{s1}'); ylabel('Magnitude (dB)');
title('(e) FFT Filtered Sine');
subplot(3,4,6); % (f) FFT of filtered random
random_fft = 20*log10(abs(fftshift(fft(random_bwlimited))));
plot(f_norm, random_fft, 'k');
xlim([-0.5 0.5]); ylim([-200 0]); grid;
xlabel('Frequency f_{s1}'); ylabel('Magnitude (dB)');
title('(f) FFT Filtered Random');
subplot(3,4,7); % (g) FFT of decimated sine
sin_dec_fft = 20*log10(abs(fftshift(fft(sin_decimated))));
plot(f_norm_dec, sin_dec_fft, 'k');
xlim([-0.5 0.5]); ylim([-200 0]); grid;
xlabel('Frequency f_{s2}'); ylabel('Magnitude (dB)');
title('(g) FFT Decimated Sine (M=5)');
subplot(3,4,8); % (h) FFT of decimated random
random_dec_fft = 20*log10(abs(fftshift(fft(random_decimated))));
plot(f_norm_dec, random_dec_fft, 'k');
xlim([-0.5 0.5]); ylim([-200 0]); grid;
xlabel('Frequency f_{s2}'); ylabel('Magnitude (dB)');
title('(h) FFT Decimated Random (M=5)');
% Row 3: Upsampled time domain and FFT
subplot(3,4,9); % (i) Upsampled sine time domain
stem(sin_interpolated, 'o', 'MarkerSize', 2);
xlim([200 250]); ylim([-1.5 1.5]); grid;
xlabel('Discrete time (n)'); ylabel('Amplitude');
title('(i) Interpolated Sine (L=5)');
subplot(3,4,10); % (j) Upsampled random time domain
stem(random_interpolated, 'o', 'MarkerSize', 2);
xlim([200 250]); ylim([-1.5 1.5]); grid;
xlabel('Discrete time (n)'); ylabel('Amplitude');
title('(j) Interpolated Random (L=5)');
subplot(3,4,11); % (k) FFT of upsampled sine
sin_up_fft = 20*log10(abs(fftshift(fft(sin_interpolated))));
plot(f_norm_up, sin_up_fft, 'k');
xlim([-0.5 0.5]); ylim([-200 0]); grid;
xlabel('Frequency f_{s1}'); ylabel('Magnitude (dB)');
title('(k) FFT Interpolated Sine');
subplot(3,4,12); % (l) FFT of upsampled random
random_up_fft = 20*log10(abs(fftshift(fft(random_interpolated))));
plot(f_norm_up, random_up_fft, 'k');
xlim([-0.5 0.5]); ylim([-200 0]); grid;
xlabel('Frequency f_{s1}'); ylabel('Magnitude (dB)');
title('(l) FFT Interpolated Random');
sgtitle('Decimation (M=5) and Interpolation (L=5) Demo');