How can we threshold a signal in both positive and negative sides?
조회 수: 12 (최근 30일)
이전 댓글 표시
I have a signal with values in both positive and negative sides. I want to threshold in both sides. I have used this code:
threshold=0.5
for i=1:length(signal)
if signal>threshold && -signal>0.5
signal_new(i)=signal
else
signal_new(i)=0
end
end
But I got signal with only positive side. Can anyone help me with thresholding at both sides?
댓글 수: 0
채택된 답변
Star Strider
2022년 9월 16일
I am not certain what you want as the result.
The for loop and if block are not necessary. You can do this with logical indexing. The logical indexing here are:
(signal_new >= threshold)
(signal_new <= -threshold)
Try this —
Fs = 256;
t = linspace(0, 1000, 1001)/Fs;
signal = sin(2*pi*t*2);
threshold = 0.5;
signal_new = signal;
signal_new(signal_new >= threshold) = threshold;
signal_new(signal_new <= -threshold) = -threshold;
figure
plot(t, signal)
hold on
plot(t, signal_new)
hold off
grid
xlabel('Time')
ylabel('Amplitude')
legend('Original','Threshold', 'Location','best')
.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Denoising and Compression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!