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 .
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
> %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)
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.