How can I Compare a Signal with some Threshold Value as comparator to give output as 0 or 1?

i want to Compare random sine singnal with some threshold value, ust as a comparator working. I want the output to be 1 if its above the Threshold Value or0 if its below threshold value.
Can somebody kindly check why is this code not working?
% Random Sine Signal to compare with Threshold Value:
Fs = 8000; % samples per second
dt = 1 / Fs; % seconds per sample
StopTime = 0.25; % seconds
t = (0: dt: StopTime-dt) '; % seconds
%% Sine wave:
Fc = 60; % hertz
x = cos (2 * pi * Fc * t);
out = zeros (size (t)); % Array to Show output less then ot greater then Threshold Value
for k = 1: 1: numel (t)
if (x> 0.5) % comparin signal with Thresold lets suppose 0.5
out (k) = 1; % If True
else
out (k) = 0; % If false
end
end
plot (t, x)
hold on
plot (t, out)

Answers (1)

Subscript ‘x’ as well to use the loop:
out = zeros (size (t)); % Array to Show output less then ot greater then Threshold Value
for k = 1: 1: numel (t)
if (x(k)> 0.5) % comparin signal with Thresold lets suppose 0.5
out (k) = 1; % If True
else
out (k) = 0; % If false
end
end
however this is much more efficient and produces the same result:
out = x > 0.5;
.

6 Comments

@Star Strider
Thankyou That works fine.
I have to plot it for mili volt signals so i need 0.001 inplace of 1. What if i hae to replace the array with 0.001 inplace of 1? I tried to multiply but that does not work, i guess?
If you want ‘out’ to be either 0 or 0.001, this will work with both your loop and my logical vector:
out = out * 0.001;
The problem is that the 1 mV signal is difficult to see on that plot.
One option would be:
out = out * 0.001;
figure
yyaxis left
plot (t, x)
yyaxis right
plot (t, out)
The yyaxis function was introduced in R2016a. If you have an earlier version, use plotyy with different syntax.
.
I tried it hard. The IF ELSE inside FOR LOOP works for sine wave but not for my signal, not sure why.
Can you please give it a look , the part where for loop is used? However ,your loic commented below for loop works. Can you relate both of them. I actually needs FOR loop becauase I have to put 2 conditions:
if x > 0.3 = 1;
else if x <0.27 =0;
nfig=1;
w=logspace(3,11,1000);
f=w/(2*pi);
%%% Equivalent Input Charge
Q=5e-15;
QTIME=4e-9;
I=Q/QTIME;
to1=1e-9; % zero delay bw start and input charge
to2=to1+QTIME;
syms s
%%% CSPreamp Model
gm1=20e-3;
CF=600e-15;
CD=60e-12;
RL=50e3;
RF=40e3;
%Parameters
R1=9.9609e+03
C1=1.0308e-11;
R2=1.0090e+04;
R3=R2
%%% Shaper 2 Sizing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Gsh2=G3;
R4=9.819e3;
C2=11.496e-12;
R5=37.931e3;
%%% Shaper1 Transfer Function %%% %%% %%% %%% %%% %%% %%% %%% %%% %%% %%% %%% %
syms vin g1 rL rf cd cf vgs vocsp iin vout1 vout2 r1 r2 r3 c1 z1 c2 r4 r5 z2 s
eq1=-iin + s*cd*vgs + s*cf*(vgs-vocsp) + (vgs-vocsp)/rf;
eq2=g1*vgs + vocsp/rL + s*cf*(vocsp-vgs) + (vocsp-vgs)/rf + vocsp/(r2*z1/(r2+z1)) ;
eq3=vocsp/(r2*z1/(r2+z1)) + vout1/r3;
eq4=-z1 + r1 + 1/(s*c1);
eq5=vout1/z2 + vout2/r5;
eq6=-z2 + r4 + 1/(s*c2);
tf=solve(eq1,eq2,eq3,eq4,eq5,eq6,vgs,vocsp,vout1,vout2,z2,z1);
%%% Plot in s-Domain
color='--r';
tfvout1=subs(tf.vout2,{g1,rL,rf,cd,cf,iin,r1,r2,r3,r4,r5,c1,c2},{gm1,RL,RF,CD,CF,1,R1,R2,R3,R4,R5,C1,C2});
%%%%%%%%%%%%%%%%
wu=1/(CF*RF);
T=20/wu;
N=500;
time=1:1:N;
time=T*time/N;
syms t;
%%% Input Signal Laplace Transform
x1s=(exp(-s*to1))*1/s-(exp(-s*to2))*(1/s);
I=Q/QTIME;
x1s=I*x1s;
y1s=x1s*tfvout1;
y1t=ilaplace(y1s);
vout_csp1=subs(y1t,{t},{time});
vout_cs=vpa(-vout_csp1)
out = zeros(size(time)); %Array to Show output less then ot greater then Threshold Value
%%%% ERROR COMES DUE TO BELOW LINE, EXCEPT THAT THIS CODE WORKS FINE
for k=1:numel(t)
if ((vout_cs) > 0.003) % comparin signal with Thresold lets suppose **ERROR IS HERE**
out(k)=1; %If True
else
out(k)=0; % If false
end
end
%out=-vout_csp1>.003;
subplot(2,1,1)
grid on
hold on
plot(time*1e9,-vout_csp1,'-k','LineWidth',3);
axis ([-10 500 -inf inf])
ax = gca;
ax.YAxis.Exponent = -3;
xlabel('Time - [ns]');ylabel('Amplitude - [V]');
hold on
subplot(2,1,2)
grid on
hold on
plot(time*1e9,out,'-r','LineWidth',3);
xlabel('Time - [ns]');ylabel('Amplitude - [V]');
axis ([-10 500 -0.5 1.5])
legend('Shaper OP','DISC OP')
I have no idea where you want to put this or what you want to do with it:
if x > 0.3 = 1;
else if x <0.27 =0;
I would just do:
out = 1*(x > 0.3) + 0*(x < 0.27);
or something similar.
What is ‘out’ (or whatever you want to call it in your code) supposed to be in the gap between 0.27 and 0.3?
I have no idea what you are doing.
@star
I actually want to set a logic for comparator with hysterisis.
It has 2 Thrseholds:
One for low transition and One for High transitionn to avoid multiple crossing of threshold due to noise while crossing threshold line. But I cant find a good loic for it.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2020a

Tags

Asked:

on 19 Aug 2020

Commented:

on 24 Aug 2020

Community Treasure Hunt

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

Start Hunting!