Smooth curve in certain regions to get desired output
2 views (last 30 days)
Show older comments
I have some data. I need to smooth it somehow. I am using smooth and medfilt1 but I cannot seem to get what I am expecting.
My data is attached. raw_data is the red curve you see in the image. The gray lines are t1, t2, and t3 respectively. I want to get the green curve, but I just cannot seem to find a solution. Any help is appreciated! Thanks ahead of time.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1423468/image.png)
0 Comments
Accepted Answer
Les Beckham
on 30 Jun 2023
Edited: Les Beckham
on 30 Jun 2023
Maybe you can adapt this approach to get what you want. This uses two different moving average filters for the start and end of the signal, with no filtering on the middle part
I kind of disagree with your green line during the first oscillation or so. The average of the real signal drops down quite a bit below your green line.
load data4question.mat
plot(raw_data) % plot the original raw data
xline([t1 t2 t3], 'r--')
grid on
xlim([1600 3000])
% Apply the two different filters to the start section and the end section and
% plot the result.
smoothed_data = raw_data;
nPointsEnd = 30; % Adjust to get desired smoothing on start section
smoothed_data(1:t1) = movmean(smoothed_data(1:t1), nPointsEnd);
nPointsEnd = 400; % Adjust to get desired smoothing on end section
smoothed_data((t3-5):end) = movmean(smoothed_data((t3-5):end), nPointsEnd);
figure
plot(1:numel(raw_data), raw_data, 1:numel(raw_data), smoothed_data, 'g')
xline([t1 t2 t3], 'r--')
grid on
xlim([1600 3000])
0 Comments
More Answers (1)
S0852306
on 20 Jul 2023
Edited: S0852306
on 20 Jul 2023
You may also try using curve fitting, here's the result I got:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1438908/image.jpeg)
(I did not adjust the scale of x-axis)
Here's the code, if you want to run this script, download the n-d curve & surface fitting pack,
detailed instructions can be found on this website.
clear; clc; close all;
load('data4question.mat');
data=1:numel(raw_data); label=raw_data';
Set up model.
NN.InputAutoScaling='on'; % perform normalization of data
NN.LabelAutoScaling='on';
InSize=1; OutSize=1;
LayerStruct=[InSize,3,5,5,OutSize];
NN=Initialization(LayerStruct,NN);
Set up optimizer.
option.Solver='ADAM';
option.MaxIteration=600; option.BatchSize=500;
NN=OptimizationSolver(data,label,NN,option);
Validating and visualize results.
P=NN.Evaluate(data);
figure;
plot(label)
hold on
plot(P,'LineWidth',1)
legend('raw data','curve fitting')
You may try different model structure or solver parameters to achieve the desired curve,
hope this might help you !
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!