How to round off a curve (remove sharp corners) in matlab

2.9k Views Asked by At

Suppose there are two curves (described by two groups of data): curve 1 and curve 2. The two curves have more than two intersection points. A new curve 3 is obtained by keeping the upper part of the two curves. The problem is that there are several sharp corners at the intersection points on curve 3. How to smooth the curve by rounding off these corners using matlab?

2

There are 2 best solutions below

7
On BEST ANSWER

Apply a convolution with a lowpass filter?

n = 4; %// adjust as needed. Higher value gives more smoothing
curve3_smooth = conv(curve3, ones(1,n)/n, 'same');

A better idea, as noted by @Hoki, is to apply the filter twice: once forwards and once backwards, to make the smoothing operation symmetric. You can achieve that with filtfilt:

n = 2;
curve3_smooth = filtfilt(ones(1,n)/n, 1, curve3);
4
On

Borrowing from Luis Mendo's post and your comments in his post, if you are given 2D data where each column is a single vector and you want to filter each of the columns separately, then use filter instead. You'd specify the dimension to be 1, as you want to filter along the rows. As such:

%// adjust as needed
n = 4;
smooth_curve = filter(ones(1,n)/n, 1, u, [], 1);