clc;
fprintf('Beginning to run %s.m ...\n', mfilename);
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
y = readmatrix('a.txt');
thresholdValue = 0.6;
subplot(2, 3, 1)
plot(y, 'b.');
yline(thresholdValue, 'Color', 'r', 'LineWidth', 2);
grid on;
title('Original Noisy Signal', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
subplot(2, 3, 2)
histogram(y);
grid on;
title('Histogram of Original Signal', 'FontSize', fontSize);
xlabel('y (signal) value', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
aboveThreshold = y > 0.9;
subplot(2, 3, 3)
bar(aboveThreshold);
grid on;
title('Above Threshold (Logical Index)', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('True or False', 'FontSize', fontSize);
props = regionprops(aboveThreshold, 'Area');
allAreas = [props.Area];
subplot(2, 3, 4)
histogram(allAreas);
grid on;
title('Histogram of Spike Widths', 'FontSize', fontSize);
xlabel('Pulse Width', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
aboveThreshold2 = bwareafilt(aboveThreshold, [5, inf]);
subplot(2, 3, 5)
bar(aboveThreshold2);
grid on;
title('Filtered, Above Threshold', 'FontSize', fontSize);
aboveThreshold3 = ~bwareafilt(~aboveThreshold2, [500, inf]);
subplot(2, 3, 5)
plot(aboveThreshold3, 'LineWidth', 2);
grid on;
ylim([0, 1.2]);
title('Filtered, Above Threshold (Logical Index)', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('True or False', 'FontSize', fontSize);
meanHighValue = mean(y(aboveThreshold3))
meanLowValue = mean(y(~aboveThreshold3))
finalSignal = meanLowValue * ones(1, length(y));
finalSignal(aboveThreshold3) = meanHighValue;
subplot(2, 3, 6)
plot(finalSignal, 'LineWidth', 2);
grid on;
ylim([0, 1.2]);
title('Final, Denoised Signal', 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized'
Best Answer