Clear Filters
Clear Filters

is my code logically correct? will i get the same output when i do it manually?

15 views (last 30 days)
clear;
clc;
close all;
x = input('Enter a sequence: ');
figure(1);
subplot(3,1,1);
stem(x);
ylabel('Amplitude');
xlabel('Samples');
title('Input sequence');
% Decimation by 4
y1 = resample(x, 1, 4);
subplot(3,1,2);
stem(y1);
ylabel('Amplitude');
xlabel('Samples');
title('Decimation by 4 - Sequence');
% Interpolation by 4
y2 = resample(x, 4, 1);
subplot(3,1,3);
stem(y2);
ylabel('Amplitude');
xlabel('Samples');
title('Interpolation by 4 - Sequence');
% Sinusoidal signal
fs = 10;
N = 50;
f = 1;
n = 0:N-1;
Ts = 1/fs;
x1 = sin(2*pi*f*Ts*n);
figure(2);
% Sine signal
subplot(3,1,1);
stem(x1);
ylabel('Amplitude');
xlabel('Samples');
title('Sine signal');
% Decimation of sinusoidal signal by 4
yd_decimate = resample(x1, 1, 4);
subplot(3,1,2);
stem(yd_decimate);
ylabel('Amplitude');
xlabel('Samples');
title('Decimation by 4 - Sinusoidal signal');
% Interpolation of sinusoidal signal by 4
yd_interpolate = resample(x1, 4, 1);
subplot(3,1,3);
stem(yd_interpolate);
ylabel('Amplitude');
xlabel('Samples');
title('Interpolation by 4 - Sinusoidal signal');

Answers (1)

Mathieu NOE
Mathieu NOE on 21 May 2024 at 10:20
hello
I wondered if the exercise was more to prove that decimating then resampling (by the same factor) would give (almost) the original signal
if yes , you have to modify your code , like
% Interpolation of sinusoidal signal by 4
yd_interpolate = resample(x1, 4, 1);
must be changed into
% Interpolation of sinusoidal signal by 4
yd_interpolate = resample(yd_decimate, 4, 1);
(otherwise you are resampling the input signal not the decimated signal)
all the best
clear;
clc;
close all;
x = input('Enter a sequence: ');
figure(1);
subplot(3,1,1);
stem(x);
ylabel('Amplitude');
xlabel('Samples');
title('Input sequence');
% Decimation by 4
y1 = resample(x, 1, 4);
subplot(3,1,2);
stem(y1);
ylabel('Amplitude');
xlabel('Samples');
title('Decimation by 4 - Sequence');
% Interpolation by 4
% y2 = resample(x, 4, 1);
y2 = resample(y1, 4, 1);
subplot(3,1,3);
stem(y2);
ylabel('Amplitude');
xlabel('Samples');
title('Interpolation by 4 - Sequence');
% Sinusoidal signal
fs = 10;
N = 50;
f = 1;
n = 0:N-1;
Ts = 1/fs;
x1 = sin(2*pi*f*Ts*n);
figure(2);
% Sine signal
subplot(3,1,1);
stem(x1);
ylabel('Amplitude');
xlabel('Samples');
title('Sine signal');
% Decimation of sinusoidal signal by 4
yd_decimate = resample(x1, 1, 4);
subplot(3,1,2);
stem(yd_decimate);
ylabel('Amplitude');
xlabel('Samples');
title('Decimation by 4 - Sinusoidal signal');
% Interpolation of sinusoidal signal by 4
% yd_interpolate = resample(x1, 4, 1);
yd_interpolate = resample(yd_decimate, 4, 1);
subplot(3,1,3);
stem(yd_interpolate);
ylabel('Amplitude');
xlabel('Samples');
title('Interpolation by 4 - Sinusoidal signal');

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!