How can I smooth data around a specific region?
10 views (last 30 days)
Show older comments
Steven Manz
on 20 Jan 2023
Commented: Steven Manz
on 21 Jan 2023
I have data. It is fairly noisy just as it is. But I only really want to smooth the oscillations. In the figure attached, the dashed red line is what I hope to achieve. I assume that there may be a need for a piecewise smoothing function. But I am not sure how to accomplish this. The data is also attached for anyone that wants to play. Thank you ahead of time for any advice.

0 Comments
Accepted Answer
Image Analyst
on 20 Jan 2023
Try a median filter. It smooths out local oscillations while keeping true steps intact and steep:
s = load('matlab_question.mat');
time_ = s.time_;
ids_ = s.ids_;
plot(time_, ids_, 'b-', 'LineWidth', 2)
xlabel('Time');
ylabel('I')
grid on;
% Take median filter
smoothIds = medfilt1(ids_, 451);
hold on;
plot(time_, smoothIds, 'r--', 'LineWidth', 2)
More Answers (1)
Joel Van Sickel
on 20 Jan 2023
Here is a simple script that gives you an easy way to implement filtering on specific pieces of data. You can accomplish this by writing something that goes inside a conditional expression looking at time. I'm just creating my own time and data vector as an example.
t = 0:0.1:1;
data = sin(t);
for i = 1:length(t)
if t(i) > 0.5 && t(i) < 0.75 % only filter data for times between 0.5 and 0.75
data(i) = (data(i-1)+data(i)+data(i+1))/3
end
end
now, that just averages the 3 points next to each other, which is a very small amount of filtering, but you can replace that line with a more sophisticated filter or averaging approach.
4 Comments
Image Analyst
on 21 Jan 2023
@Alex Sha he said "I only really want to smooth the oscillations" and that formula has the oscillations still in there.
Communities
More Answers in the Power Electronics Control
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!