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

34 次查看(过去 30 天)
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

回答(2 个)

Kris Fedorenko
Kris Fedorenko 2017-8-7
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 个评论
Kris Fedorenko
Kris Fedorenko 2017-8-9
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 2019-5-13
how do i apply guided filter on csv file of hyperspectral dataset ! can anyone help me please as soon as possible .

请先登录,再进行评论。


abolfazl fardad
abolfazl fardad 2017-12-16
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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by