# Generating a Sampled Square Wave in Matlab

I'm a newbie on use of MATLAB and also in signals and systems. I need clarification and guide (with fundamental explanations please) on above.

I want to generate a sampled square wave in MATLAB with following criteria;

Signal duration $T=5\mathrm{s}$, Sample frequency $f_s=40\mathrm{Hz}$. there are supposed to be $f_sT$ signal points (what does this mean please). $\tau=1/100$. the square wave signals are zeros for the first $\frac{f_sT(1-\tau)}{2}$ points and also for the last $\frac{f_sT(1-\tau)}{2}$ points. they are one otherwise.

What I have done so far:

clear all

fs = 40;
t = [0:1/fs:5];
dc = 50;
x = square(2*pi*t);
%f=1 for a square wave

plot (t,x);


Am I right? I have no idea how to use tau, and what signal points imply here. Kindly assist. Thank you.

The word "points" means "samples." Product fs*T is fs samples/second times T seconds. That's a signal duration of 40*5 = 200 samples (or points). This means your Matlab code for vector t should have been:

t = [0:1/fs:T-1/fs];

You wrote tau = 1/100, so (1–tau) = 0.99. Ratio fsT(1-tau)/2 = 40*5*0.99/2 = 99 samples (points). You cannot have a square wave sequence of length 200 samples (points) if the first and last 99 samples (points) are zero-valued. Your fs, T, and tau variables are incompatible with each other!

Note: Your x = square(2*pi*t); Matlab command is equivalent to:

x = square(2*pi*1*t); % Note the "1"

which produces square wave that repeats every one second. If you want just one cycle of a square wave over 200 samples use the commend:

x = square(2*pi*t/5);