Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gaussian smoothing of a 1-D vector

28 views
Skip to first unread message

fdfs tank

unread,
Feb 10, 2010, 3:46:05 PM2/10/10
to
Hello,
I need to smooth a 1-D vector. In the smoothing I need to have an option to choose the width of the smoothing function (number of cells).
Does anyone know of a gaussian smoothing function?
If not, do you know a matlab code I can use to solve this?
Thank you.

ImageAnalyst

unread,
Feb 10, 2010, 4:44:39 PM2/10/10
to
conv() can do it. Just convolve your array with your Gaussian window,
of whatever length you want, and you're done.

fdfs tank

unread,
Feb 10, 2010, 10:25:12 PM2/10/10
to
How exactly do you this?
Sorry, but I have never done such smoothing before.
Can you please give me an example code of how this can be done?
Thank you.

ImageAnalyst

unread,
Feb 10, 2010, 10:57:28 PM2/10/10
to
"fdfs tank"

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

% Construct blurring window.
windowWidth = int16(5);
halfWidth = windowWidth / 2
gaussFilter = gausswin(5)
gaussFilter = gaussFilter / sum(gaussFilter); % Normalize.

% Do the blur.
smoothedVector = conv(vector, gaussFilter)

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

fdfs tank

unread,
Feb 10, 2010, 11:11:02 PM2/10/10
to
Great, it works! thank you,
A couple of questions:
What is int16? why do you need it here instead of just writing the window width as a regular scalar?
and why do you plot only the second half of the smoothed vector?
smoothedVector(halfWidth:end-halfWidth)
thank you very very much :)

fdfs tank

unread,
Feb 10, 2010, 11:24:02 PM2/10/10
to
and one more small question: what is the Gaussian Kernel (sigma) of this function? is it equivalent to the window width?

ImageAnalyst

unread,
Feb 11, 2010, 9:13:45 AM2/11/10
to
fdfs tank:
I cast it to int16 so that I could divide it by 2 and not get a
fractional number of elements for the half-window width. When you
slide a window along another array, the resulting array will of course
be longer. It will be longer than the "big" vector by the length of
the sliding window. This is called the "edge effect" or "boundary
effect" where the window is just starting to creep into the vector but
is not totally overlapping it yet. This will occur a half-window's
width into the resulting vector. I didn't take just half the entire
output. I just skipped the half-width of the sliding window where it
was only partially overlapping the main vector at the beginning and
end of the resulting vector - basically just cropped out the edge
effects.

The window width and the "spread" of the Gaussian are independent of
each other. You can set up whatever window width you want, and
calculate a Gaussian with whatever variance you want inside that. I
simple used guasswin() because it was a convenient built-in function,
but you could of course make one up yourself.
-ImageAnalyst

Yang Jingling

unread,
May 22, 2013, 4:32:08 AM5/22/13
to
Hi, I read your example codes of this gaussian smoothing filter. I have a question, if I want to define my unique gaussian smoothing function w(t), for example. How could I implement it by matlab? I still have no idea about how to change the variance in your codes. Should it be in the function gauswin(5)? can you show the whole gaussian filter codes with w(t)?

Parag Nayak

unread,
Mar 6, 2016, 7:19:13 PM3/6/16
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <18faa2e2-06e4-4434...@b2g2000yqi.googlegroups.com>...
where can i add a .csv file that consists of the data? I want to smooth the data that are stored in a .csv file

Parag Nayak

unread,
Mar 6, 2016, 7:53:08 PM3/6/16
to
can u please explain the code?
0 new messages