How do I specify a different smoothing factor to each datapoint individually?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
How do I specify a different smoothing factor to each datapoint individually? For example, for an array with 100 elements, I want the first and last 10 elements to be "fully" smoothed (smoothing factor/weight of 1), the middle elements to be unaffected (smoothing factor/weight of 0), and other elements to have a smoothing factor/weight of between 0 and 1.
1 commentaire
Réponse acceptée
Mathieu NOE
le 7 Avr 2023
hello
that was my first idea ; coding a first order recursive filter with variable forgetting factor (or pole)
doing it forward and backward like filtfilt to avoid phase distorsion
the "window" or shape for the varaible filter is made with a inverted bell (gaussian) shape , but you can give it another shape if your prefer.
samples = 100;
x = 1:samples;
y = sin(x/10)+0.3*rand(size(x));
s = 0.75; % smoothing factor
window = (1-exp(-(x-samples/2).^2/300));
%%%%%%%%%%%%%%%%
% "smart filter" : simple first order low pass filter
% the smoothing factor varies with time
%% main loop
alpha = s*window;
% forward filtering
ys = zeros(size(y));
ys(1) = y(1);
for ci = 2:samples
% first order low pass IIR filter recursion
ys(ci) = alpha(ci).*ys(ci-1) + (1-alpha(ci)).*y(ci);
end
% backward filtering
ysflipped = ys(samples:-1:1);
alphaflipped = alpha(samples:-1:1);
ys = zeros(size(y));
ys(1) = ysflipped(1);
for ci = 2:samples
% first order low pass IIR filter recursion
ys(ci) = alphaflipped(ci).*ys(ci-1) + (1-alphaflipped(ci)).*ysflipped(ci);
end
ys = ys(samples:-1:1); % flip the output
figure(1)
subplot(2,1,1)
plot(x,window);
title('Blending Factor' );
subplot(2,1,2)
plot(x,y,x,ys);legend('Raw','Smoothed');
title('Data Smoothed with home made filter' );
then I thought maybe I can achieve the same result in a shorter code , try simply to blend (with a variable factor) the raw data and a smoothed version of it
basically the two results are very similar
Now you play with the parameters of smoothdata AND the blending ratio
enjoy !
samples = 100;
x = 1:samples;
y = sin(x/10)+0.3*rand(size(x));
ys = smoothdata(y,'gaussian',15);
window = (1-exp(-(x-samples/2).^2/300));
yss = window.*ys + (1-window).*y; % blending the smoothed and non smoothed data according to window weight
figure(1)
subplot(2,1,1)
plot(x,window);
title('Blending Factor' );
subplot(2,1,2)
plot(x,y,x,yss);legend('Raw','Smoothed');
title('Data Smoothed with modified smoothdata filter' );
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Descriptive Statistics dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!