MATLAB Answers

how to apply Gaussian filter to imported data (two columns: x and y)

90 views (last 30 days)
Tian Tian
Tian Tian on 3 Aug 2017
Commented: Kirti Gaur on 13 May 2019
Hi,
I am new to Matlab,and what i want to do is: import two columns of data (x and y) into Matlab, apply Gaussian filter on plot graph drawn from data, then find peaks of y data with corresponding x located. Now my problem is: when importing to Matlab, column labels of data got lost, so I couldn't use peak analysis on specified column; also, is there any way to apply Gaussian on plot graph?
Many thanks for your answer. Tian

  1 Comment

Sign in to comment.

Answers (2)

Kris Fedorenko
Kris Fedorenko on 7 Aug 2017
Hi Tian!
I am not sure if I quite understood all of your question, but here is what I think you want to do:
  1. import two columns of data preserving the column names (x, y)
  2. plot the data
  3. apply Gaussian filter to smooth the data in y and plot it
  4. use the smoothed data to find local peaks in y and the corresponding x values
If I understood your question correctly, this simple example might be helpful:
%%1. import two columns of data preserving the column names (x, y)
% Assuming you have a .csv or an excel file with two numeric columns named x and y,
% you can use "readtable" to import your data
data = readtable('sample_data.csv')
% you can now access x values as data.x and y values as data.y
%%2. plot the data
figure;
plot(data.x, '*-') % plot x as stars
hold on;
plot(data.y, 'o-') % and y as circles
hold off;
%%3. apply Gaussian filter to smooth the data in y and plot it
% create filter
sigma = 10; % pick sigma value for the gaussian
gaussFilter = gausswin(6*sigma + 1)';
gaussFilter = gaussFilter / sum(gaussFilter); % normalize
filteredY = conv(data.y, gaussFilter, 'same');
% plot filtered y and original y to see the effect:
figure;
plot(data.y, 'o-r') % plot original y as cirlces
hold on;
plot(filteredY, 'b') % and filtered y as a solid line
hold off;
%%4. use the smoothed data to find local peaks in y and the corresponding x values
[peak_values, peak_indices] = findpeaks(filteredY);
% use peak's indices to find corresponding x values:
corresp_x = data.x(peak_indices)
Hope this helps!
Kris

  5 Comments

Show 2 older comments
Tian Tian
Tian Tian on 9 Aug 2017
Hi Kris,
My situation is a little bit different. First, I need to plot x to y (as two axis), and Gaussian-smooth y which can be regarded as 2 Gaussian peaks (like mountains, I would like to apply Gaussian to each simulated peak ),then pick out the two or more peaks in the Gaussian-smoothed y. Do you know how to apply multiple Gaussian in this situation? Thank you.
Kris Fedorenko
Kris Fedorenko on 9 Aug 2017
Hi Tian!
I am still a bit unclear about your question, but sounds like you might be interested in fitting multiple gaussians to your data? In this case you might consider looking into the following documentation pages:
As for plotting your y data versus your x data, you can do it as follows:
plot(data.x, data.y)
Kirti Gaur
Kirti Gaur on 13 May 2019
how do i apply guided filter on csv file of hyperspectral dataset ! can anyone help me please as soon as possible .

Sign in to comment.


abolfazl fardad
abolfazl fardad on 16 Dec 2017
Hi Kris,
Thank you for giving your codes. Would you please explain why you find gausswin for (6*sigma+1):
gaussFilter = gausswin(6*sigma + 1)';
Ned

  0 Comments

Sign in to comment.

Sign in to answer this question.