# MATLAB square wave

Discussion in 'Programmer's Corner' started by thedolphin13, Sep 21, 2010.

1. ### thedolphin13 Thread Starter New Member

Sep 21, 2010
2
0
Hello smart people...I'm a CS major who has to take CPET lab. There is one lab project that is well out of the scope of my understaning...it could have just as well been written in Chinese.

We have already used (and I guess are supposed to modify) this code:

clear;
clf;
t = 0:0.001:1; % Time vector
e = sin(2*pi*60*t);
figure(1), stem(e(1:30));
figure(2), stem(e(1:30)), grid on;
figure(3), plot(t,e), grid on,
figure(4), plot(t(1:50),e(1:50)), grid on;
figure(5), plot(t(1:100),e(1:100)), grid on;

As mentioned, this is so far out of the scope of my understanding that I do not even know what the questions are asking or how to set-up the code to display results.

Thank you so much for your contribution. Also, and this is an aside, is there a MATLAB function that would give the size of a period. I believe the period in the above sample code is 1/60 = .0167, but how would I get MATLAB to tell me that from the console window.

Again, Thank you so much

2. ### Georacer Moderator

Nov 25, 2009
5,150
1,271
I don't understand the parts of the exercise either, but the point of it is on reconstructing the square wave from its harmonics. Matlab help contains a relative demo under the name "Square Wave from Sine Waves". Search it in the Help file.

What troubles me is that, if I understand correctly, you are asked to construct a square wavetrain of finite duration. That could be problematic. Perhaps the exercise meant only to create it for this duration, that is, not to plot any longer. That is duable.

The square wave formula gives you the appropriate amplitude and frequency for each harmonic. The more harmonics you add to the result, the more accurately it will resemble to a square wave.

That concludes the constructive part of the exercise. The Questions are the ones that give me a hard time though.

• The highest order of harmonic doesn't exist. They are infinite. Maybe the question asks for something different but fails to put it into words.
• Question b asks for the time that is needed to sample one full period of the signal. That is the time that the basic harmonic needs to complete one cycle.
• The sampling frequency of 10kHz seems fine to me, since it is more that twice the frequency of your higher harmonic (13*60=780Hz). But someone please verify this.
This is all I can do for now. If someone points out something different, that might get my wheels moving.

3. ### thedolphin13 Thread Starter New Member

Sep 21, 2010
2
0
Thanks for the reply. I actually purchased the MATLAB software to discover it was only going to be used in this one lab. What a shame. Anyway, I have forwarded your reply (credit to you) with questions of my own to the prof to show that I have indeed researched the problem...this is just not worth the few points I will get for it. Thanks again for your effort.

4. ### Georacer Moderator

Nov 25, 2009
5,150
1,271
Thanks for the credit! I really appreciate it.