time | calls | line |
---|
| | 1 | function s = smooth_gaussian(data,sigma,size)
|
| | 2 |
|
| | 3 | % data : input vector with raw data.
|
| | 4 | % sigma : standard deviation of the gaussian distribution used in the smoothing.
|
| | 5 | % size : size of vector over which smoothing function is applied. (2-3 sigmas is usually good.)
|
| | 6 |
|
| | 7 | % Gaussian smoothing.
|
| 40 | 8 | halfsize = round(size/2);
|
| 40 | 9 | a = 1/(sqrt(2*pi)*sigma);
|
| 40 | 10 | b = 1/(2*sigma^2);
|
| 40 | 11 | w = a*exp(-b*(-halfsize:1:halfsize).^2);
|
| 40 | 12 | w = w/sum(w); % normalize the filter to a total of 1.
|
| | 13 |
|
| | 14 | % Extends endpoint data to larger than smoothing width.
|
| 40 | 15 | data_L_val = data(1);
|
| 40 | 16 | data_R_val = data(end);
|
| 40 | 17 | data_L = ones(1,size*4)*data_L_val;
|
| 40 | 18 | data_R = ones(1,size*4)*data_R_val;
|
| 40 | 19 | data = [data_L data data_R];
|
| | 20 |
|
| | 21 | % filters data and shifts smoothing left to align with data.
|
| 40 | 22 | filtered = circshift(filter(w,1,data),[1 -halfsize]);
|
| | 23 |
|
| | 24 | % crops away the extended endpoint data from the raw and smoothed data.
|
| 40 | 25 | filtered(1:size*4) = [];
|
| 40 | 26 | filtered((end-size*4+1):end) = [];
|
| 40 | 27 | data(1:size*4) = [];
|
| 40 | 28 | data((end-size*4+1):end) = [];
|
| 40 | 29 | s = filtered;
|