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

4 ビュー (過去 30 日間)
Syed Adeel
Syed Adeel 2020 年 8 月 19 日
コメント済み: Star Strider 2020 年 8 月 24 日
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)

回答 (1 件)

Star Strider
Star Strider 2020 年 8 月 19 日
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 件のコメント
Syed Adeel
Syed Adeel 2020 年 8 月 24 日
@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.
Star Strider
Star Strider 2020 年 8 月 24 日
I have no idea how to interpret that diagram.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by