MATLAB: Constant Amplitude from a Sine Wave

simpowersystemsSimscape Electricalsimulink

I am trying to transform a normal 50Hz sine wave (frequency will not change) into a constant amplitude value. I have done so with an RMS block in SimPowerSystems. But I am wondering what way would you do it? Any suggestions.

Best Answer

hi Mark,
So you are talking about square wave or Pulse train, there are many ways to generate or derive that signal from Cos/Sin wave :
Let us derive an example and derive the signal by two ways :
Fs=120;
t=0:1/Fs:1;
F=50;% Hz
%Phi=1.33;
y=2*sin(t*F);
1.Solution 1 : using a loop :
n=length(y);
Pulse_train=zeros(1,n);
Max=max(y);
Min=min(y);
%y(y>0)=Max;
%y(y<0)=Min;
for x=1:n
if y(x)>0
Pulse_train(x)=Max;
elseif y(x)<0
Pulse_train(x)=Min;
else
Pulse_train(x)=y(x);
end
end
figure, plot(t,Pulse_train), axis ([0 1 -3 3])
2.Using Built-in Mathw(c) function :
y2=2*square(t*F);
figure, plot(t,y2), axis ([0 1 -3 3]), hold on, plot(t,Pulse_train,'r')
There are many ways , either by using the software or using Electronic devices , and by the way take a look at this Mathw(c) Example that illustrates :The Fourier series expansion for a square-wave altough this method has the disadvantage of Gibbs effect : http://www.mathworks.com/products/matlab/examples.html?file=/products/demos/shipping/matlab/xfourier.html