how to generate square wave in matlab

조회 수: 341 (최근 30일)
md
md 2014년 6월 16일
댓글: Vedanta Mohapatra 2021년 10월 1일
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.

채택된 답변

Image Analyst
Image Analyst 2014년 6월 16일
편집: Image Analyst 2020년 11월 24일
Make one period, then use repmat() to replicate as many times as you want.
% Now make one cycle with amplitude 1000 and 200 elements.
onePeriod = 1000 * [ones(1, 100), zeros(1, 100)];
% 200 elements is supposed to be 0.1 seconds so find out delta t.
dt = 0.1 / numel(onePeriod);
% Define number of cycles.
numCycles = 10;
% Copy this period that many times.
fullWaveForm = repmat(onePeriod, [1, numCycles]);
% Make time axis from 0 to 1 second.
t = dt * (0 : (numel(fullWaveForm) - 1));
% Now plot it.
plot(t, fullWaveForm, 'b-', 'LineWidth', 2);
FontSize = 20;
xlabel('Time (seconds)', 'FontSize', FontSize);
ylabel('Amplitude', 'FontSize', FontSize);
title('Square Wave With 10 Cycles', 'FontSize', FontSize);
grid on;
  댓글 수: 2
TANMAYA KAANR
TANMAYA KAANR 2017년 6월 15일
Just Put a saturation block with lower limit zero. It will give only Positive value.
Image Analyst
Image Analyst 2017년 6월 15일
I don't know what that means. What is a "saturation block"? Is that Simulink? The poster said "in matlab" but if your function is for Simulink, then perhaps your post will help Simulink users.

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Rashmil Dahanayake
Rashmil Dahanayake 2014년 6월 16일
t=0:.001:1;
f=10;
sq=1000*0.5*(square(2*pi*f*t)+1);
plot(t,sq)

Bill Tubbs
Bill Tubbs 2020년 3월 26일
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
udhaya ram mohan 2016년 10월 18일
i need to generate the square wave with positive cycle only please send me a code for it
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 10월 18일
Rashmil Dahanayake's Answer already only generates positive values.

댓글을 달려면 로그인하십시오.


pisini padmaja
pisini padmaja 2020년 11월 23일
편집: Image Analyst 2020년 11월 24일
%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
Image Analyst
Image Analyst 2020년 11월 24일
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
Vedanta Mohapatra 2021년 10월 1일
You have to use signum to generate some sort of square wave

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Language Fundamentals에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by