Question

I am trying to smooth some measurement data in Matlab and I think so, I am missing something. I wrote my own code to create the Gaussian window. However rest of code is copied from this link:

% Generate sample data.
vector = 5*(1+cosd(1:3:180)) + 2 * rand(1, 60);
hist (vector)  ;
plot(vector, 'r-', 'linewidth', 3);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

% Construct blurring window.
windowWidth = int16(11);
halfWidth = windowWidth / 2 ;
gaussFilter = Gaussain (-5:5, 0, 1 ) ;
gaussFilter = gaussFilter / sum(gaussFilter); % Normalize.

% Do the blur.
smoothedVector = conv(vector(halfWidth:end-halfWidth), gaussFilter) ;

% plot it.
hold on;
plot(smoothedVector, 'b-', 'linewidth', 3);

Please help me correct my mistake. Below is the code for generating Gaussain Window:

function y = Gaussain ( window, mu, sigma) 
% y = exp (-((window - mu).^2)/(2*sigma^2)).* (1/(sigma * sqrt(2* pi)))  ;
y = exp (-((window - mu).^2)/(2*sigma^2)) ;
end

I am looking for a solution which does not use any Matlab toolkit's method. After fixing few things, I am getting this output:enter image description here

Was it helpful?

Solution

It looks like WindowWidth should be int16(11), to match your filter. Or you could use WindowWidth = int16(50) with a filter that has 50 taps, instead of 11. With current values you are trimming too much of your output signal.

See figure with WindowWidth = int16(11): http://i.stack.imgur.com/WOnW9.png

enter image description here

OTHER TIPS

I was investigating in the same topic, founding this post that solved my problem, doing some adjustments and generalizations. With many thanks to the author of the previous reply, here is my generalized code, that works also with n-dimension points vectors

% SMOOTH Simple gaussian smooth function
% vector = input vector
% width = width of the smoothing
% window = width of the window to be used in the convolution
% 
% Credits: Generalization of a post made by User1551892 and Louis Mendo on 
% StackOverflow
% Created by: Leonardo Daga, 2016
function smoothedVector = smooth(vector, width, window)

% Check arguments and fill with defaults
if nargin < 2,
    width = 2;
end

if nargin < 3,
    window = width * 5 + 1;
end

% Rotate vector to a column vector (if needed) to make the code easier
if size(vector,1) > size(vector,2)
    isColumn = true;
else
    isColumn = false;
    vector = vector';
end

% Construct blurring window.
windowWidthInt = int16(window);
halfWidth = double(windowWidthInt / 2);
gaussFilter = Gaussian(-(halfWidth-1):(halfWidth-1), 0, width/2 ) ;
gaussFilter = gaussFilter / sum(gaussFilter);

% Do the blur, enlarging the vector to blur from the start with a
% convenient value and cutting the vector back when the blurring is done
enlargedVector = [ones(window, 1)*vector(1,:);
    vector;
    ones(window, 1)*vector(end,:)];

smoothedVector = zeros(size(enlargedVector,1)-1,size(enlargedVector,2));

for i=1:size(vector,2),
    smoothedVector(:,i) = conv(enlargedVector(halfWidth:end-halfWidth,i), gaussFilter) ;
end

smoothedVector = smoothedVector(window:end-window,:);

% Rotate back the vector, if it was a row vector
if (isColumn == false)
    smoothedVector = smoothedVector';
end

end

%% Create a gaussian vector to be used for the convolution
function y = Gaussian ( window, mu, sigma)
y = exp (-((window - mu).^2)/(2*sigma^2)) ;
end

You can run this function with the following sample code:

% Generate sample data.
vector = [5*(1+cosd(1:3:900)) + 2 * rand(1, 300);
    5*(1+sind(1:3:900)) + 2 * rand(1, 300)];

smoothedVector = smooth(vector, 5) ;

% plot it.
figure
plot(1:size(vector,2), vector, 'r-', 1:size(smoothedVector,2), smoothedVector, 'b-');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

getting the figure that follows:

Smoothed cosine and sine with gaussian blur

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top