0
$\begingroup$

I have data like the following:

x = [0,0,1,1.98999999999978,2.99000000000069,3.98000000000047,14.0500000000002,15.0599999999995,15.8700000000008,15.8700000000008,16.9399999999996,17.8900000000003,18.8800000000001,29.7900000000000,30.8299999999999,31.8700000000008,32.8699999999999,32.8699999999999,33.8299999999999,34.8599999999997,35.8199999999997,46.0400000000000,46.7600000000002,47.8100000000004,48.8000000000002,48.8000000000002,49.7600000000002,50.7500000000009,62.0400000000000,62.7900000000009,63.7900000000000,64.8000000000002,65.8000000000002,65.8000000000002,66.7800000000007,67.7900000000000,78.0900000000002,78.8999999999996,79.9000000000006,80.9000000000006,81.8999999999996,81.8999999999996,82.8899999999994,83.8900000000003,94.0900000000002,94.5500000000002,95.6400000000003,96.5500000000002,96.5500000000002,97.5500000000002,98.5700000000006,109.560000000000,110.510000000000,111.500000000000,111.500000000000,112.500000000001,113.500000000000,113.500000000000,114.520000000000,126.090000000000,126.090000000000,127.050000000000,127.050000000000,128.050000000000,128.050000000000,129.050000000000,130.040000000000,141.540000000000,142.570000000000,142.570000000000,143.530000000000,144.120000000000,144.120000000000,145.310000000000,157.330000000000,158.320000000001,159.320000000000,160.070000000000,160.610000000000,160.610000000000,161.660000000000];
y = [0,0,0.464601238374681,0.929755484255206,1.39546164308871,1.86171861902337,2.79588063231313,3.73223273143554,4.20122730982987,5.14084800633951,6.08263671695609,7.02658457720498,8.44653533211063,9.39584215120541,10.3472769347812,11.3008307275720,12.7351149203275,13.6939269261834,14.6548264413097,16.1000700040090,16.5828518024014,18.0342831256240,18.5191184145376,19.4903194207480,20.4635533783748,20.9509298520746,21.9271960550294,22.9054722725899,23.8857492968480,24.3766352288654,25.8522688397480,25.8522688397480,26.3451347901984,26.8384928487593,27.8266806453806,28.3215080581393,28.8168229284115,29.3126240906732,29.8089103782565,30.3056806233516,30.8029336570103,30.8029336570103,31.3006683091478,31.7988834085470,31.7988834085470,32.2975777828593,32.2975777828593,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,31.7988834085470,31.3006683091478,30.3056806233516,29.3126240906732,27.8266806453806,26.8384928487593,24.8680179010629,22.9054722725899,21.4388111269700,19.0044642269341,17.5499595010577,15.1360561892542,11.3008307275720,8.92092218717511,6.08263671695609,3.26378347149450,1.86171861902337,1.39546164308871,1.39546164308871,1.39546164308871];

plot(x, y);

The actual visualization of the data is as on the left of the image below. However, I want to smooth the data like the one on the right. enter image description here Any suggestions/thoughts? Thank you.

$\endgroup$

2 Answers 2

2
$\begingroup$

Here are a few options:

  1. Lowpass Filtering: LPF

  2. Smoothing: Smooth

  3. Savitzky-Golay Filtering: SG

Here is the overall comparison:

Overall

Note that these are representative plots. You can tune the filtering parameters to get better results. Or you could concatenate the results of two operations to get a better overall result.

Here is the code:

clear; close all; clc;

x_lpf = lowpass(x,0.03); x_smooth = smoothdata(x); x_sg = sgolayfilt(x,3,9);

figure(1) plot(x, y, 'r','LineWidth',2); hold on plot(x_lpf,y, 'y','LineWidth',2); hold on plot(x_smooth,y, 'g','LineWidth',2); hold on plot(x_sg,y, 'b','LineWidth',2); legend('Original','Lowpass Filtered', 'Smoothing','S-G Filtered') legend('boxoff') box off set(gcf, "color", "w");

figure(2) plot(x, y, 'r','LineWidth',2); hold on plot(x_lpf,y, 'y','LineWidth',2); legend('Original','Lowpass Filtered') legend('boxoff') box off set(gcf, "color", "w");

figure(3) plot(x, y, 'r','LineWidth',2); hold on plot(x_smooth,y, 'g','LineWidth',2); legend('Original','Smoothing') legend('boxoff') box off set(gcf, "color", "w");

figure(4) plot(x, y, 'r','LineWidth',2); hold on plot(x_sg,y, 'b','LineWidth',2); legend('Original','S-G Filtered') legend('boxoff') box off set(gcf, "color", "w");
$\endgroup$
1
  • $\begingroup$ I think this is is a generally good answer, but in this case it just wallpapers over the underlying problem. It makes' the picture look better but I personally find it hard to interpret filtering on a very non-uniform sampling grid. $\endgroup$
    – Hilmar
    Jun 10, 2023 at 13:38
3
$\begingroup$

Here is the easy version

enter image description here

I created this by simply ignoring the X-axis and assuming the data is uniformly sampled.

I think there is a problem with your sampling grid. It's very non-linear with large more or less periodic jumps in it. There are also duplicates in the x-axis that are NOT duplicates in the y-axis, which is physically impossible. That's very unusual and partially non-sensical) for a sampling grid and that's exactly why your data looks so funky.

I'm guessing something went wrong when importing or exporting the sampling grid.

%% data
x = [0,0,1,1.98999999999978,2.99000000000069,3.98000000000047,14.0500000000002,15.0599999999995,15.8700000000008,15.8700000000008,16.9399999999996,17.8900000000003,18.8800000000001,29.7900000000000,30.8299999999999,31.8700000000008,32.8699999999999,32.8699999999999,33.8299999999999,34.8599999999997,35.8199999999997,46.0400000000000,46.7600000000002,47.8100000000004,48.8000000000002,48.8000000000002,49.7600000000002,50.7500000000009,62.0400000000000,62.7900000000009,63.7900000000000,64.8000000000002,65.8000000000002,65.8000000000002,66.7800000000007,67.7900000000000,78.0900000000002,78.8999999999996,79.9000000000006,80.9000000000006,81.8999999999996,81.8999999999996,82.8899999999994,83.8900000000003,94.0900000000002,94.5500000000002,95.6400000000003,96.5500000000002,96.5500000000002,97.5500000000002,98.5700000000006,109.560000000000,110.510000000000,111.500000000000,111.500000000000,112.500000000001,113.500000000000,113.500000000000,114.520000000000,126.090000000000,126.090000000000,127.050000000000,127.050000000000,128.050000000000,128.050000000000,129.050000000000,130.040000000000,141.540000000000,142.570000000000,142.570000000000,143.530000000000,144.120000000000,144.120000000000,145.310000000000,157.330000000000,158.320000000001,159.320000000000,160.070000000000,160.610000000000,160.610000000000,161.660000000000];
y = [0,0,0.464601238374681,0.929755484255206,1.39546164308871,1.86171861902337,2.79588063231313,3.73223273143554,4.20122730982987,5.14084800633951,6.08263671695609,7.02658457720498,8.44653533211063,9.39584215120541,10.3472769347812,11.3008307275720,12.7351149203275,13.6939269261834,14.6548264413097,16.1000700040090,16.5828518024014,18.0342831256240,18.5191184145376,19.4903194207480,20.4635533783748,20.9509298520746,21.9271960550294,22.9054722725899,23.8857492968480,24.3766352288654,25.8522688397480,25.8522688397480,26.3451347901984,26.8384928487593,27.8266806453806,28.3215080581393,28.8168229284115,29.3126240906732,29.8089103782565,30.3056806233516,30.8029336570103,30.8029336570103,31.3006683091478,31.7988834085470,31.7988834085470,32.2975777828593,32.2975777828593,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,32.7967502586087,31.7988834085470,31.3006683091478,30.3056806233516,29.3126240906732,27.8266806453806,26.8384928487593,24.8680179010629,22.9054722725899,21.4388111269700,19.0044642269341,17.5499595010577,15.1360561892542,11.3008307275720,8.92092218717511,6.08263671695609,3.26378347149450,1.86171861902337,1.39546164308871,1.39546164308871,1.39546164308871];
clf
plot(x, y);


%% Try 1
hold on
n = length(x);
x1 = linspace(0,x(end),n);
plot(x1,y,'Linewidth',2);
legend('original','smoothed');
grid('on')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.