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

38 views (last 30 days)

Show older comments

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

### Answers (2)

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:

- import two columns of data preserving the column names (x, y)
- plot the data
- apply Gaussian filter to smooth the data in y and plot it
- 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

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:

https://www.mathworks.com/help/curvefit/gaussian.html https://www.mathworks.com/help/stats/fitgmdist.html

As for plotting your y data versus your x data, you can do it as follows:

plot(data.x, data.y)

Kirti Gaur
on 13 May 2019

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

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!