1D gaussian filter without "fainting edges"

44 visualizaciones (últimos 30 días)
Jonas
Jonas el 2 de Abr. de 2021
Respondida: Image Analyst el 2 de Abr. de 2021
The convolution method "conv" is great and simple to implement a Gaussian filter, see e.g. this answer. However, it naturally leads to "fainting edges", because the convolution is the sum of a product, and at the edges have fewer elements. Here an example:
% my test data:
data = 2*ones(1,100);
% create the gaussian filter:
sigma = 10; % pick sigma value for the gaussian
gaussFilter = gausswin(6*sigma + 1)';
gaussFilter = gaussFilter / sum(gaussFilter); % normalize
% use the convolution:
filteredData = conv(data, gaussFilter, 'same');
% display result:
figure(1)
plot(filteredData, 'LineWidth', 5)
grid on
xlabel('Index')
ylabel('Filtered data')
Changing 'same' to an other option does not do the trick: The edges are either removed or they are "fainting". Thus, it seams natural to ask, if there are clever methods to scale the weigths? Of course, one can simply loop over the edges and scale the weights properly,
%%
% first n data-points:
hGauss = @(i) 1/sqrt(2*pi*sigma^2)*exp(-1/2 *(i/sigma).^2); % gaussian
for i = 1:n
weigths = hGauss((-i+1):n);
weigths = weigths / mean(weigths); % normalize
filteredData(i) = mean(data(1:(n+i)) .* weigths);
end
and analog for the end. However, this is rather ugly and slow. What methods do you use to obtain proper edges?

Respuestas (1)

Image Analyst
Image Analyst el 2 de Abr. de 2021
imfilter() gives you additional options for handling edge effect problems.
Option
Description
Padding Options
numeric scalar, X
Input array values outside the bounds of the array are assigned the value X. When no padding option is specified, the default is 0.
'symmetric'
Input array values outside the bounds of the array are computed by mirror-reflecting the array across the array border.
'replicate'
Input array values outside the bounds of the array are assumed to equal the nearest array border value.
'circular'
Input array values outside the bounds of the array are computed by implicitly assuming the input array is periodic.

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by