Why do upsample and downsample commands give wrong results in start of sequence?

9 次查看(过去 30 天)
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')

采纳的回答

Chunru
Chunru 2021-11-9
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')

更多回答(2 个)

dpb
dpb 2021-11-9
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.

Mathieu NOE
Mathieu NOE 2021-11-9
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 个评论
Sadiq Akbar
Sadiq Akbar 2021-11-9
Thanks to all of you. But I have to accept one answer. As the chunru has responded 1st, and that works too, so I am going to accept his answer. Thanks once again to all of you.
Mathieu NOE
Mathieu NOE 2021-11-9
ok
I will survive even though on my screen it appears that I answered 1 hour before Chunru
argh!!!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by