How can I filter/remove peaks in my curve? (not a signal)

10 vues (au cours des 30 derniers jours)
Manuela Garcia
Manuela Garcia le 30 Déc 2019
Commenté : Star Strider le 4 Jan 2020
Hi all.
I am trying to remove the peaks from my data using the Signal Processing Toolbox, without success.
In my data, which values are attached, there are peaks which values are significantly lower than the "correct data" at the beginning, and afterwards I have peaks with both higher and lower values. Also, there are infinite values which are not plotted and I want to ignore.
I have tried different commands but as I am quite new to MatLab and my curve is not a signal, I am not sure if I am using the ToolBox correctly.
Every help will be very much appreciated. Thank you very much.
P.S. I know the image is from Excel but of course I am managing the data in MatLab.

Réponse acceptée

Star Strider
Star Strider le 30 Déc 2019
It is not obvious what result you want. If you just want a regression line through your data, first use the fillmissing (R2016b and later) function to eliminate the non-finite gaps (first replace the Inf values with NaN), then use polyfit and polyval to do a simple regression and evaluation.
I have not looked at your data because I do not know what you want to do. However the best approach to filter design is first to do a Fourier transform (fft) on your data to understand its spectral characteristics, then design your filter to elimiinate the unwanted frequencies. Your data have to be regularly-sampled (constant sampling interval) of none of these will work correctly. If they do not have a constant sampling interval, use the resample function (after fillmissing) first to correct that.
  8 commentaires
Manuela Garcia
Manuela Garcia le 4 Jan 2020
Hey again,
This might be a stupid question, but as you helped me so much I thought it was worth asking!
As you can see in the plot, I get the "blue points" which are the initial data and the "red line" which is the solution after applying polyval and polyfit, if I correctly understood your code.
My problem is, when plotting Yp2 I get these two curves, connecting points with different spacings, and I would like to have only the one which connects all points. I tried to look in MatLab answers but can't find this specific case.
Thank you very much again! Your help saves me a lot of time!
plot.jpg
Star Strider
Star Strider le 4 Jan 2020
One problem I noticed with the data set you posted was that the data doubles back on itself. If you plot it as a line instead of points, that is easily apparent.
I am not certain how you want to deal with that problem.
One way might be to sort the data by the x-values, although with some of your data that might not be the most optimal solution.
Another way would be to only use the initial data to the maximum value of the x-vector (not the last value).
Using your posted ‘data.mat’, that would be:
D = load('data.mat');
x = D.data(:,1);
y = D.data(:,2);
y(isinf(y)) = NaN; % Replace ‘Inf’ with ‘NaN’
y2 = fillmissing(y, 'linear'); % Interpolate ‘NaN’ Values
[max_x,idx] = max(x); % Value & Index Of Maximum X-Value
xe = x(1:idx); % Trim Vector
y2e = y2(1:idx); % Trim Vector
P1 = polyfit(xe, y2e, 5); % Fit For Detrending
Yp1 = polyval(P1, xe); % Evaluated Fit For Detrending
y3e = y2e - Yp1; % Detrend
[y4e,TF] = rmoutliers(y3e, 'median'); % Remove Outliers From Detrended Data
yclean = y4e + Yp1(~TF); % Reconstitute Original Data
P2 = polyfit(xe(~TF), yclean, 7); % Fit ‘Cleaned’ Data To Arbitrary Polynomial
Yp2 = polyval(P2, xe); % Evaluate Fit Of ‘Cleaned’ Data To Arbitrary Polynomial With Original Data
figure
plot(xe, y2e, '.')
hold on
plot(xe, Yp2, '-r')
% plot(x(~TF), yclean)
hold off
grid
ylim([-1E+3 1E+4])
That seems to work well with this data set.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by