Gaussian smoothing of time series
65 views (last 30 days)
Show older comments
I have a time series with measurements taken at time t along with measurement uncertainties. I would like to smooth this data with a Gaussian function using for example, 10 day smoothing time.
How could this be done?
Thank you
0 Comments
Accepted Answer
Wayne King
on 16 May 2013
You do not tell us how many samples represents 10 days in your t variable. That is an important piece of missing information. Here, I'll just assume that t is in days and you have 1 sample per day. You'll have to adjust accordingly if that is not accurate.
If you have the Signal Processing Toolbox, you can use gausswin
x = randn(1000,1);
w = gausswin(10);
y = filter(w,1,x);
3 Comments
Xen
on 25 Apr 2018
Please correct me if I am wrong, but the accepted answer has a problem. The gaussian window is not normalized, thus your filtered vector will have larger values than expected. I created a window of length 5 and this essentially doubled the amplitude of my vector. w must have a unit sum:
w = w/sum(w);
Chinmayee L M
on 1 Aug 2021
I ran to the same problem. It is not normalised. But, can you please explain the normalisation that you have suggested? How is that a normalisation?
This doesn't take into account the length and width of the window. I want a normalisation factor that accounts for the length and width of the window.
More Answers (2)
Image Analyst
on 16 May 2013
If you don't have the Signal Processing Toolbox, make up your weighted window, then use conv():
filteredSignal = conv(originalSignal, gaussianWindow);
1 Comment
Ashraful Haque
on 18 May 2020
Hey I know this comment is from a long time ago. But Hopefully you see my reply. My question was how do I create a gaussian window function without the signal processing toolbox? What are the input(s) and output(s)?
Andreas Pagel
on 5 Aug 2021
I had a similar issue with some 'random' noise spikes on the signal which I wanted to eliminate. The usual smoothing and moving avg approaches I found when searching for solutions did not match my expectations as the noise would still distord the results.
So, I created some kind of of a gauss filter, using a scatter recording approach.:
the code is still a bit rough but does it's job.
I get 10 readings, record them in an array thay counts how ofter a given value is found and pull then the value that got most hits;
#define sampleSize 10
int sampleArray[sampleSize + 1][2] = {{0}}; // initialise array
Sorry for being lazy...
I know it's not optimal but I like the easy way of using real references for adressing the array, ie 1st record sits in array[1]
push () records the values and increments the counter for a given value:
void push (int val) {
short i = 0;
for (i = 1; i <= sampleSize; i++) {
if (debug) Serial.printf("%i-%i: %i #%i\n", i, val, sampleArray[i][0], sampleArray[i][1]);
if (sampleArray[i][0] == val) {
++sampleArray[i][1];
return;
}
else if (sampleArray[i][0] == 0) {
sampleArray[i][0] = val;
++sampleArray[i][1];
return;
}
else if (i == sampleSize) Serial.printf("ERROR - too many values: %i\n", i);
}
}
and pull() returns the value with most hits:
int pull() {
int maxCnt = 0;
int maxVal = 0;
short i = 0;
for (i = 1; i <= sampleSize; i++) {
if (sampleArray[i][1] > maxCnt) {
maxCnt = sampleArray[i][1];
maxVal = sampleArray[i][0];
}
}
return maxVal;
}
since I wanted to simplify the tests, I did not even use a data source but simply used random numbers.
int readValue() {
short i = 0;
// reinit array
for (i = 1; i <= sampleSize; i++) {
sampleArray[i][0] = 0;
sampleArray[i][1] = 0;
}
for (i = 1; i <= sampleSize; i++) {
//push(AnalogRead(ADC));
push(random(20, 35));
}
return pull();
}
my main loop calls the readValue() and sends it to an IoT cloud - now lukily with out anymore with the spikes I ahd before.
I use the code now in different sketches for data capturing and it works perfectly fine :-)
0 Comments
See Also
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!