how to generate square wave in matlab
Ältere Kommentare anzeigen
Hi,
I intend to generate a square wave with respect to time by following characteristic:
I want to generate square-wave force demand of 1 KN at 10 Hz. That means amplitude will be between 0 to 1000 and time will be between 0 to 1 (sec) with .1 interval.
Please someone help me.
Akzeptierte Antwort
Weitere Antworten (4)
Rashmil Dahanayake
am 16 Jun. 2014
t=0:.001:1;
f=10;
sq=1000*0.5*(square(2*pi*f*t)+1);
plot(t,sq)
Bill Tubbs
am 26 Mär. 2020
Based on Rashmil Dahanayake's idea I made a simple function to generate regular square waves for discrete time.
function sq = square_dt(n,period)
% Square wave in discrete time
repeats = n/period;
sq = square(2*pi*linspace(0,repeats-1/n,n));
end
(Works best when period is an even number)
Example
>> square_dt(10,4)
ans =
1 1 -1 -1 1 1 -1 -1 1 1
If you want only the positive cycles, modify the code like Rashmil's:
function sq = square_dtp(n,period)
% Square wave in discrete time
repeats = n/period;
sq = (square(2*pi*linspace(0,repeats-1/n,n))+1)/2;
end
Example
>> square_dtp(10,4)
ans =
1 1 0 0 1 1 0 0 1 1
udhaya ram mohan
am 18 Okt. 2016
0 Stimmen
i need to generate the square wave with positive cycle only please send me a code for it
1 Kommentar
Walter Roberson
am 18 Okt. 2016
Rashmil Dahanayake's Answer already only generates positive values.
pisini padmaja
am 23 Nov. 2020
Bearbeitet: Image Analyst
am 24 Nov. 2020
%square function%
clc;
clear all;
close all;
t=0:0.01:2;
x=sin(2*pi*t);
subplot(2,1,1);
plot(t,x,'g');
xlabel('time');
ylabel('amplitude');
title('square signal 318126512095');
subplot(2,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('square sequence 318126512095');
2 Kommentare
Image Analyst
am 24 Nov. 2020
This does not look like an Answer for how to create a square wave. Here is what your code produces:
%square function%
clc;
clear all;
close all;
t=0:0.01:2;
x=sin(2*pi*t);
subplot(2,1,1);
plot(t,x,'g');
xlabel('time');
ylabel('amplitude');
title('square signal 318126512095');
subplot(2,1,2);
stem(t,x,'r');
xlabel('time');
ylabel('amplitude');
title('square sequence 318126512095');

Vedanta Mohapatra
am 1 Okt. 2021
You have to use signum to generate some sort of square wave
Kategorien
Mehr zu Spectral Measurements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
