Why do upsample and downsample commands give wrong results in start of sequence?
4 views (last 30 days)
Show older comments
I have a code for upsampling an input sequnce x and then downsampling it back to original by a factor 3. When I look at the plot of input sequence x, it starts at 1 though in DSP it must start at n=0. Likewise when I look at the plot of upsampled sequence y, it must also start at n=0 and then insert two zeros in between every two samples of x. But its not so here in the plots. Why it is so?
% Up/Down Sampler
clear all
close all
clc
x=[1 2 3 4]; % Input Sequence
y=upsample(x,3); % Upsampled sequence y by a factor of 3
subplot(4,1,1)
stem(x)
title('Input Sequence')
subplot(4,1,2)
stem(y)
title('Up sampled sequence')
c=downsample(y,3); % downsampled sequence y again by the same factor 3
subplot(4,1,3)
stem(c)
title('Down sampled sequence')
0 Comments
Accepted Answer
Chunru
on 9 Nov 2021
All time axes start from 0.
% Up/Down Sampler
clear all
close all
clc
x=[1 2 3 4]; % Input Sequence
y=upsample(x,3); % Upsampled sequence y by a factor of 3
subplot(4,1,1)
stem(0:numel(x)-1, x)
title('Input Sequence')
subplot(4,1,2)
stem(0:numel(y)-1, y)
title('Up sampled sequence')
c=downsample(y,3); % downsampled sequence y again by the same factor 3
subplot(4,1,3)
stem(0:numel(c)-1, c)
title('Down sampled sequence')
0 Comments
More Answers (2)
dpb
on 9 Nov 2021
Don't know what the complaint is, precisely?
upsample and downsample are working as documented --
>> upsample(1:4,3)
ans =
1 0 0 2 0 0 3 0 0 4 0 0
>> downsample(ans,3)
ans =
1 2 3 4
>>
"y = upsample(x,n) increases the sample rate of x by inserting n – 1 zeros between samples." from the documentation for upsample which is precisely what you see above -- and downsample recreates the original input series.
What's to complain about?
If the "issue" is that the plots are shown on the x axis as from 1:N instead of 0:N-1, that's because you plotted them without providing an abscissa vector in which case (also as documented) stem and all the other MATLAB plotting functions plot the provided y values against the ordinal position in the array.
Also as documented, MATLAB array indexing is one-based, NOT zero-based and is immutable/unchangeable by the user. If you want to plot versus some other x, then you must provide that
N=numel(y)-1;
stem(0:N,y)
xlim([0 N])
xticks([0:11])
may be more what you want.
0 Comments
Mathieu NOE
on 9 Nov 2021
hello
Iprefered somehow to create an explicit x axis for each data
now I think the plot is better because evrything is aligned as expected
% Up/Down Sampler
clear all
close all
clc
y = [1 2 3 4]; % Input Sequence
x = (0:length(y)-1);
yu=upsample(y,3); % Upsampled sequence y by a factor of 3
xu = (0:length(yu)-1);
yc=downsample(yu,3); % downsampled sequence y again by the same factor 3
xc = (0:length(yc)-1);
subplot(3,1,1)
stem(x,y)
title('Input Sequence')
subplot(3,1,2)
stem(xu,yu)
title('Up sampled sequence')
c=downsample(y,3); % downsampled sequence y again by the same factor 3
subplot(3,1,3)
stem(xc,yc)
title('Down sampled sequence')
2 Comments
Mathieu NOE
on 9 Nov 2021
ok
I will survive even though on my screen it appears that I answered 1 hour before Chunru
argh!!!
See Also
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!