In Matlab Help-> Signal Processing toolbox->waveform generation only show
x=Square(t);
however i fail to generate a square plot with this 1 line of coding, actually how to define t? how can I input the frequency to this plot? and also sampling frequency?
can u show me a sample coding to help me understand it?
Regards,
Joey
x = [0 1 2 2 3 4 5 5 6 7 8]
y = [0 0 0 5 5 5 5 0 0 0 0]
plot(x,y,'-x')
"Joey W" <joey...@hotmail.com> wrote in message <hgq853$fs7$1...@fred.mathworks.com>...
Hi Joey, did you read
>>doc square
?
The square wave works just like sin(t) and cos(t) except that it takes value +1 and -1.
a 100 Hz square wave
t = 0:0.001:0.1; % Sampling frequency 1 kHz
y = square(2*pi*100*t);
plot(t,y);
axis([0 0.1 -1.5 1.5]);
xlabel('Seconds');
ylabel('Amplitude');
Hope that helps,
Wayne
Dear Wayne,
Thanks so so so much!! it really helps.. i can understand already..
if there is a sum of signals, it will be y= square(2*pi*100*t)+ square(2*pi*2000*t); ?
is it correct? if i want to show combination of frequencies..
How to see the above example is taking 1KHz as sampling frequency?
thank you so much!
Regards,
Joey
1.) that square waves are not sine waves so you won't just be contributing a single frequency component per square wave. In fact, you should see that you get odd harmonics, e.g. a square wave of 100 Hz will have peaks at 100, 300, 500, Hz
2.) you have to increase your sampling rate, decrease your sampling interval, if you want a square wave at 2 kHz as you've written above. You can't do that if you keep your sampling interval at 0.001.
Hope that helps,
Wayne
Dear Wayne,
Really thanks for your explanation..I have got better understanding on the square wave.
As u said a square wave of 100 Hz will have peaks at 100, 300, 500, Hz, so just now i tried to plot "amplitude versus frequency" to visualize it myself, but the coding i wrote ,cant generate the output as what we want, can you please correct my coding?
t = 0:0.001:0.1;
f= 100;
y = square(2*pi*f*t);
plot(t,y);
axis([0 0.1 -1.5 1.5]);
xlabel('Seconds');
ylabel('Amplitude');
X=fft(y);
subplot (2,1,2), plot (f,abs(X));
xlabel('Frequency');
ylabel('amplitude');
You need a proper frequency vector. You have declared f as a scalar and used it as the frequency of the square wave and then you try to use the same variable as a frequency vector in plotting the absolute value of the Fourier transform, but you need a vector of frequencies in the latter case.
Wayne