Create a 10 Khz square wave in Matlab.

Discussion in 'Programmer's Corner' started by ynaji, Feb 9, 2008.

1. ynaji Thread Starter Member

Jan 28, 2007
10
0
Can you please help ? I am trying to plot a square wave with 10 khz frequency. I am able to plot a square wave with 2 cycle below is the code.

n = 0:255;
t = n/256;
x = [ ones(1,64),-ones(1,64), ones(1,64), -ones(1,64)];
plot(t,x);

2. Papabravo Expert

Feb 24, 2006
12,272
2,721
Looks good to me. Why do you think the frequency is or is not 10 kHz?

3. ynaji Thread Starter Member

Jan 28, 2007
10
0
There is nothing wrong with code. I know how to do 2 cycle square wave and not 10 Khz square wave. The code can do 2 cycle square wave . but the assignement required 10 khz.

4. Papabravo Expert

Feb 24, 2006
12,272
2,721
So what properties would a 10 kHz square wave have in matlab that your two cycle squarewave does not have? If you say on your plot that the horizontal axis represnets 200 microseconds and that each division in your array represents (200/256) microseconds then you are home free. What is the major malfunction here? I don't get it.

5. Dave Retired Moderator

Nov 17, 2003
6,960
171
The problem with generating the square wave in Matlab in the way above is that the result is retained within a matrix. Therefore for an arbitrarily long signal, you need an arbitrary sized matrix.

You need to specify what the signal length would be and code accordingly. What is the required signal length?

Dave