# How can I smooth data around a specific region?

10 views (last 30 days)
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.

Image Analyst on 20 Jan 2023
Try a median filter. It smooths out local oscillations while keeping true steps intact and steep:
s = struct with fields:
time_: [2437×1 double] ids_: [2437×1 double]
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)
Steven Manz on 21 Jan 2023
@Image Analyst Referring to all other comments, your first one (medfilt1) is exactly what I needed. I needed oscilations removed. And medfilt1 did this perfectly.

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.
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

### Categories

Find more on Smoothing and Denoising in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by