I've created a function to detect white edges in an image. This plot is a segment in an image (post processing of my function). The example above has a lot of noise in it, as you can see the segment I'm trying to plot is quite consistent along the 220 mark (y axis) and the large peaks (mostly above) represent noise, there are a few below also. Is there a function or some sort of way I can remove these large peaks? the function works quite well for the majority of images I'm using, data such as above is one of an awkward bunch.
Neeta Dsouza answered .
2025-11-20
To remove noise from a plot, you can use various filtering techniques. Here are a few common methods you can use in MATLAB:
1. **Moving Average Filter**: This smooths the data by averaging a subset of data points.
2. **Low-pass Filter**: This removes high-frequency noise by applying a low-pass filter.
3. **Savitzky-Golay Filter**: This smooths the data while preserving the shape of the signal.
### Example: Removing Noise Using a Moving Average Filter
```matlab
% Sample data with noise
t = 0:0.01:1;
y = sin(2*pi*5*t) + 0.5*randn(size(t)); % Signal with noise
% Moving average filter
windowSize = 5;
y_smooth = filter(ones(1, windowSize)/windowSize, 1, y);
% Plot original and smoothed data
figure;
plot(t, y, 'r', t, y_smooth, 'b');
legend('Noisy Signal', 'Smoothed Signal');
title('Noise Removal Using Moving Average Filter');
xlabel('Time');
ylabel('Amplitude');
```
### Example: Removing Noise Using a Low-pass Filter
```matlab
% Sample data with noise
t = 0:0.01:1;
y = sin(2*pi*5*t) + 0.5*randn(size(t)); % Signal with noise
% Design a low-pass filter
fs = 100; % Sampling frequency
fc = 10; % Cut-off frequency
[b, a] = butter(6, fc/(fs/2)); % 6th order Butterworth filter
% Apply low-pass filter
y_filtered = filtfilt(b, a, y);
% Plot original and filtered data
figure;
plot(t, y, 'r', t, y_filtered, 'b');
legend('Noisy Signal', 'Filtered Signal');
title('Noise Removal Using Low-pass Filter');
xlabel('Time');
ylabel('Amplitude');
```
### Example: Removing Noise Using Savitzky-Golay Filter
```matlab
% Sample data with noise
t = 0:0.01:1;
y = sin(2*pi*5*t) + 0.5*randn(size(t)); % Signal with noise
% Apply Savitzky-Golay filter
windowSize = 11; % Odd number
polynomialOrder = 3;
y_smooth = sgolayfilt(y, polynomialOrder, windowSize);
% Plot original and smoothed data
figure;
plot(t, y, 'r', t, y_smooth, 'b');
legend('Noisy Signal', 'Smoothed Signal');
title('Noise Removal Using Savitzky-Golay Filter');
xlabel('Time');
ylabel('Amplitude');
```
Choose the method that best suits your data and the type of noise you are dealing with. Let me know if you need further help!