Gaussian moving average on dataset with replicate analyses

Hey Guys,
I got isotopic abundance data of samples that come from a different period in the past, but sometimes I have more than 1 measurement per sample/time. I want to smooth my data using a gaussian window but I can't figure out how to smooth my data in the time domain.
minimum working example. % create a matrix with replicate time values, probably not import but let's say that time here is days
time = [1 1 2 3 3 3 4 4 4 4 5 6 6 6 6 6 7 7 8 8 9 9 9 10];
% create associated values
data = [0.43 0.44 0.45 0.44 0.46 0.48 0.49 0.50 0.49 0.52 0.55 0.52 0.51 0.53 0.52 0.55 0.49 0.49 0.46 0.45 0.44 0.40 0.41 0.35]
%create 5-day gaussian window and normalize
w = gausswin(5)/sum(gausswin(5))
Now I want to use this gaussian window to smooth my data set by moving it with a 1 day timestep. But I want all replicates to be incorporated. I also would like to add a conditional, so that it skips a day when there aren't enough replicates in the window.
Does anyone have an idea on how to do this?

Respuestas (1)

If you want to apply gaussian window smoothing on the data matrix then you can use conv with one vector flipped.
smoothedData = conv(data, fliplr(w))
smoothedData = smoothedData(length(w):end-length(w)+1); % to only get the data points in which the window fully fits the |Data| matrix.

7 comentarios

Hey Ameer,
This doesn't move the window by 1 day but just by 1 replicate. I want the mid point of the window to skip from 1 to 2 in the time matrix. So for a fully fitting window this would result in 6 returned values (for day 3 to 8 in this working example). The main problem that i'm having: let's say for the first fully fitting window, I want the values in the time matrix that correspond to 1 to be multiplied by w(1), 2 by (w2), 3 by (w3), 4 by w(4) and 5 by w(5). These should then be normalised based on the number of replicate measurements in the window and their weights. After that I want the window to move to a value +1 day in the time matrix and do the same thing for that window.
It will be easier to understand if you give an example that how you want to calculate the first element of the final matrix. For example, the first day that fit the window is day 3. What is your expected output for day 3?
Robin Vorsselmans
Robin Vorsselmans el 20 de Mayo de 2018
Editada: Robin Vorsselmans el 20 de Mayo de 2018
so for the day 3 it would be: (w(1)*data(1)+w(1)*data(2)+ w(2)*data(3)+w(3)*data(4)+w(3)*data(5)+w(3)*data(6)+w(4)*data(7)+w(4)*data(8)+w(4)*data(9)+w(4)*data(10)+w(5)*data(11))/(2*w(1)+1*w(2)+3*w(3)+4*w(4)+1*w(5))
so that would be 0.4730
Then the window should shift one position to the right :) thx for trying to figure this out with me
Ameer, why flipped? A Gaussian is symmetric.
Robin, if the element spacing is not one day then you'll have to figure out how many elements it is. If the spacing is not uniform or you do not have the same number of elements per day then you might have to interpolate/resample your data to get uniform spacing.
Ameer Hamza
Ameer Hamza el 20 de Mayo de 2018
Editada: Ameer Hamza el 20 de Mayo de 2018
Thanks, @Image Analyst, for pointing this out. I overlooked this point.
@Robin, How are you sliding the window? For the sliding window, each weight should appear an equal number of time. For example, at first time instant
[1 1 2 3 3 3 4 4 4 4 5 6 6 6 6 6 7 7 8 8 9 9 9 10];
w1 w2 w3 w4 w5
which gives
w(1)*data(1) + w(2)*data(2) + w(3)*data(3) + w(4)*data(4) + w(5)*data(5)
then at second time instant
[1 1 2 3 3 3 4 4 4 4 5 6 6 6 6 6 7 7 8 8 9 9 9 10];
w1 w2 w3 w4 w5
which gives
w(1)*data(2) + w(2)*data(3) + w(3)*data(4) + w(4)*data(5) + w(5)*data(6)
so each from w(1) to w(5) weights will occur in each window. And in the final equation for day 3 there should be an equal number of w(1), w(2)... w(5).
that's what I did initially but I actually want all the data points of the same date to get the same weight and then move the window by one day.
it's allright guys, I made the data equally spaced and built a monster of a loop. The dataset isn't too big so this should do. thx for all the help

Iniciar sesión para comentar.

Categorías

Más información sobre Chemistry en Centro de ayuda y File Exchange.

Preguntada:

el 20 de Mayo de 2018

Comentada:

el 20 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by