Eliminating the noise from the raw data using median filter
Show older comments
Hi everyone,
I have a raw csv file data and when I am to plot it, it seems pretty noisy. What I wanted to do ıs either the entire signal(data); or just along the decay curve to be smoothened; in other words; deleting the noise as much as possible. I'd be appreciated if anyone could help me out.
Here's the code that I am dealing with. Apart from reading the csv file, I also come across a similar code on the community so I was trying to modify it.
% Remove quiet parts of a signal.
clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 25;
data= readmatrix('T0027CH1.CSV');
x=data(:,1);
y=data(:,2);
y=(y+35.6);
x=(x+0.8*10^(-3));
N = 125013; % length(col1) if doing the whole signal
col1 = data(1:N, 1);
subplot(2, 2, 1);
plot(col1,y,'b-')
grid on
title('Original Signal', 'FontSize', fontSize);
subplot(2, 2, 2);
col1a = medfilt1(abs(col1),50000);
plot(col1a,y,'b-')
title('After median filter', 'FontSize', fontSize);
threshold = 50;
yline(threshold, 'Color', 'r', 'LineWidth', 2)
grid on;
badIndexes = col1a < threshold;
subplot(2, 2, 3);
plot(badIndexes,y, 'b-', 'LineWidth', 2);
title('Bad parts', 'FontSize', fontSize);
grid on;
% Remove bad indexes.
col1(badIndexes) = [];
subplot(2, 2, 4);
plot(col1,'b-')
grid on;
title('Bad parts removed', 'FontSize', fontSize);
2 Comments
Mathieu NOE
on 28 Nov 2022
hi
to better help you we need the data file as well
N/A
on 28 Nov 2022
Answers (1)
Mathieu NOE
on 28 Nov 2022
hello again
is it now smooth enough ?

% Remove quiet parts of a signal.
clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 25;
data= readmatrix('T0027CH1.CSV');
x=data(:,1);
y=data(:,2);
y=(y+35.6);
x=(x+0.8*10^(-3));
% removing x NaNs
idx = ~isnan(x);
x = x(idx);
y = y(idx);
ys = smoothdata(y,'gaussian',2500);
plot(x,y,'b',x,ys,'r')
legend('raw','smoothed');
4 Comments
N/A
on 28 Nov 2022
Mathieu NOE
on 29 Nov 2022
hello again
I have nothing against medfilt
FYI, smoothdata will do the same job if you use the option 'movmedian'
here I tested different options , I let you choose which one is your prefered ;
the median filter cannot filter out the small quantization noise , in this aspect the gaussain or movmean is better; for the large spikes around t = 12 , movmedian is a better approach; you can also smooth the staircase aspect of the movmedian filter by using a second layer of filtering;
the possibilities are endless and smoothdata offers many options that are not available in other functions; there is still to explore if you want
% Remove quiet parts of a signal.
clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 25;
data= readmatrix('T0027CH1.CSV');
x=data(:,1);
y=data(:,2);
y=(y+35.6);
x=(x+0.8*10^(-3));
% removing x NaNs
idx = ~isnan(x);
x = x(idx);
y = y(idx);
ys1 = smoothdata(y,'gaussian',2500);
ys2 = smoothdata(y,'movmedian',2500);
ys3 = smoothdata(ys2,'movmean',2500);
plot(x,y,'b',x,ys1,'r',x,ys2,'c',x,ys3,'m')
legend('raw','gaussian smoothed','movmedian smoothed','movmedian + movmean smoothed');
Zoom around t = 0

Zoom around t = 12 e -4 s

Mathieu NOE
on 6 Dec 2022
hello
problem solved ?
Mathieu NOE
on 31 Jan 2023
hello again
If my submission fullfills your request, do you mind accepting it ?
tx
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!