Peter123
Peter123

Reputation: 567

Generating a signal that transforms from sine to rectangular shape using Matlab

I want to generate a signal that transforms from sine to rectangular shape with an increasing parameter f using Matlab.

For f = 0, the signal sould be a sine whereas for f = 1, the signal should be a rectangular signal with the same frequency. For increasing values between 0 and 1, the signal should become increasing similar to a rectangular signal.

Unfortunately, I don't know how to realise that using Matlab. Does anyone have an idea how to do that?

My idea was to use the Fourier series of a rectangular signal and - dependent on the parameter f - to consider a distinct number of summands of it.

Upvotes: 0

Views: 559

Answers (1)

Wolfie
Wolfie

Reputation: 30175

You can generate a purely square wave with period 2π using square.

You could do a weighted average of the square wave and sin wave, as an alternative to signal clipping as suggested by Luis in the comments.

t = 0:0.1:2*pi;
hold on
f = 0;    % entirely sine wave
plot(t, (square(t)*f + sin(t)*(1-f)))
f = 0.5;  % half and half
plot(t, (square(t)*f + sin(t)*(1-f)))
f = 1;    % entirely square wave
plot(t, (square(t)*f + sin(t)*(1-f)))

Output:

wave

Upvotes: 1

Related Questions