André Diniz
André Diniz

Reputation: 11

Matlab plot half-wave rectifier

folks. For my college project, I need to plot a half-wave rectifier with the sum of two sine waves. Thus, I have chosen MATLAB to use as a tool, but I am having this problem (after the code):

l=[0:10^-6:1/1500];
sig=8*sin(2*pi*100000*l)+6*sin(2*pi*10000*l);
subplot(211)
plot(sig);

for t=1:667
if (8.*sin(2.*pi.*100000.*l)+6.*sin(2.*pi.*10000.*l))<=0
sig(t)=0;
else
sig(t) = 2.*sin((2.*pi.*100000*l + 2.*pi.*10000*l)/2).*cos(2.*pi.*100000*l - 2.*pi.*10000*l);
end
end

The problem showed on the command screen is: "In an assignment A(:) = B, the number of elements in A and B must be the same". How do I solve this issue?

Upvotes: 1

Views: 2824

Answers (1)

eyllanesc
eyllanesc

Reputation: 244162

To obtain the rectified signal there are several forms, but the simplest and most compact way is to use the matrices, in this case it is the following:

l=[0:10^-6:1/1500];
sig = 8*sin(2*pi*100000*l)+6*sin(2*pi*10000*l);
sig_rect = sig.*(sig >= 0);
subplot(211)
plot(sig) 
subplot(212)
plot(sig_rect) 

enter image description here

If you want to use loops you must do the following:

sig_rect = zeros(length(sig));

for t=1:sig
    if sig(t) <=0
        sig_rect(t) = 0;
    else
        sig_rect(t) = sig(t);
    end
end

Upvotes: 1

Related Questions