Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sampling Theorem in MATLAB

101 views
Skip to first unread message

nicollete

unread,
Dec 31, 2009, 2:08:38 PM12/31/09
to
i all,

I have just started out with my signals and systems course and as an
exercise I am trying to do visualise the sampling theorem on MATLAB
Hence I need to do something as simple as multiplying a continous
signal with an impulse train. I am struggling to write the code .
Please help me!!

I can plot the sine/cosine signal and the impulse train separately but
cannot multiply them Sad

% Cosine from -pi to pi
t1 = linspace(0, 6 , 1000);
x = cos( 2* pi * t);

%Impulse train
t2 = linspace ( 0,6,7 );
train = ones(1,t2);

% sampledsig = x.*train;

Now when I try to multiply them , I am always getting an error which
is obvious since the vector size is different . Any help is greatly
appreciated .

Message has been deleted
Message has been deleted
Message has been deleted

nicollete

unread,
Jan 1, 2010, 1:05:02 AM1/1/10
to
On Jan 1, 4:13 am, ImageAnalyst <imageanal...@mailinator.com> wrote:
> You might put "hold on" before the last plot if you want to see both
> the stem and the cosine curve plotted at the same time on the same
> plot.  As it is, your plot() blows away your stem().

How do I solve it ? any clues? It turns out for me a good knowledge of
MATLAB is turning out to be a bigger hindrance :( Any help is
appreciated.

Thanks & Regards

ImageAnalyst

unread,
Jan 1, 2010, 1:20:32 AM1/1/10
to
Solve what? Solve your plot blowing away your stem? I told you - use
the hold function.

Matt J

unread,
Jan 1, 2010, 1:43:05 AM1/1/10
to
nicollete <newbi...@gmail.com> wrote in message <b265fd47-bd57-4e76...@m25g2000yqc.googlegroups.com>...
> On Jan 1, 12:23 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> > nicollete <newbie.u...@gmail.com> wrote in message <16001d03-5455-4782-85e9-8170e689a...@a32g2000yqm.googlegroups.com>...

> > > Now when I try to multiply them , I am always getting an error which
> > > is obvious since the vector size is different . Any help is greatly
> > > appreciated .
> >
> > No, you're getting errors because the code you've shown cannot generate either x or train:

> >
> > >> % Cosine from -pi to pi
> >
> > t1 = linspace(0, 6 , 1000);
> > x = cos( 2* pi * t);
> >
> > ??? Undefined function or variable 't'.

> >
> > >> %Impulse train
> >
> > t2 = linspace ( 0,6,7 );
> > train = ones(1,t2)
> >
> > Warning: Input arguments must be scalar.
> >
> > train =
> >
> >    Empty matrix: 1-by-0
>
> Oh wait! I should have copy-pasted instead of typing it .My bad
>
> % Impulse train :
> >> t = linspace(0,6,7);
> >> train = ones(1,length(t));
> >> stem(t,train)

> %Cosine
> >> t1 = linspace(0,6,1000);
> >> x = cos(2*pi*t1);
> >> plot(t1,x)


It's not clear what spacing you want between the impulses in train. If you want them spaced N apart, then you could construct it using something like

train=~mod(1:length(x),N)

nanren888

unread,
Jan 1, 2010, 3:24:02 PM1/1/10
to
nicollete <newbi...@gmail.com> wrote in message <16001d03-5455-4782...@a32g2000yqm.googlegroups.com>...

Well a bit of a mixture of replies.
I suspect that your problem is in a different area, so I'll start differently.
It seems that, as you said, Matlab is the issue. We should deal with that. Also as pointed out, if you want two plots, use figure(1); plot something; figure(2); plot something else. Or use
plot something
hold('on');
plot other thing
hold('off');
But, the issue: Your different lengths are really just a side-effect. There are a couple of different way you can approach it. You want to multiply a (almost continuous) waveform by a unit sampling train. If you really must do that multiply, then you must have the two things at the same time instances. Then they really will be the same length as well. So taking that way;
The third parameters are effectively a sample rate. Lets choose this. By the way "7" is a really small number. I'm not sure you'll see clearly the effect of aliasing on one cycle. :)
sR = 7;
Now choose the sample rate for the "continuous" wave to be a multiple, so that the sample land exactly on values.
ratio = 100;
sRhigh = ratio*sR;
I'll ignore any problems with the odd (N-1) for clarity. Sometimes as there is a sample at the beginning & at the end, you need to fix a few last ones.


> % Cosine from -pi to pi

>t1 = linspace(0, 6 , sRhigh);
> x = cos( 2* pi * t1);
Now the sampling pulse train is mostly zeros over the same time?
> train = zeros(size(t1));
But has a few unit samples
train(1:ratio:end) = 1;
Have a look to see if I got this close
figure(1);
plot(t1,x);
figure(2);
stem(t1,train);
hold('off');

Then you should be in a good position to do that multiply.

The fuzz at the bottom is likely all those zeros. You might want to experiment with
things like
plotMask = (train>0);
stem(t1(plotMask),train(plotMask));

For that multiply, you want, if it doesn't go right you might try the matlab operator .* for element-wise multiplication.

Ok, this way leaves you with a little messy control over sample rates & your sampling on the "continuous" waveform keeps changing, but that's not the point anyway.
There are other way, if you don't like working from the sample rate.
Hope that helps.

0 new messages