How can I find peak from matrix

조회 수: 8 (최근 30일)
Phudit Kanittasut
Phudit Kanittasut 2021년 5월 1일
답변: Image Analyst 2021년 5월 1일
clear
pure_brain = readmatrix('Pure Brain Spectra.csv');
[pks,locsBr] = findpeaks(pure_brain(:,2));
pure_Liver = readmatrix('Pure Liver Spectra.csv');
[pks,locsLiv] = findpeaks(pure_Liver(:,2));
figure
plot(xBrain(locsLiv(LvBrain)), YBrain, '.r')
hold on
plot(xLiver(locsLiv(LvLiver)), YLiver, '.b')
hold off
grid
% To find common peaks considering the threshold of 1E+4:
lB = locsBr(LvBrain);
lL = locsLiv(LvLiver);
s = max(length(lB), length(lL));
locsLivNew = [lL; false(s-length(lL),1)];
locsBrNew = [lB; false(s-length(lB),1)];
common = ismember(lB,lL);
I want to find peak from the data I have. From the screenshot the first column is X coordinate and the other are Y coordinate for each spectrum. I want to find peak spot for each spectrum

답변 (2개)

dpb
dpb 2021년 5월 1일
What's wrong with what you've done for the one?
findpeaks is one of the functions that has not been vectorized (owing to its complexity, no doubt) to operate by column on an input array by default.
So, just write a loop and iterate over the columns of interest...
pure_brain = readmatrix('Pure Brain Spectra.csv');
pure_Liver = readmatrix('Pure Liver Spectra.csv');
for i=2:size(pure_brain,2)
[pksBr,locsBr] = findpeaks(pure_brain(:,i));
[pksLiv,locsLiv] = findpeaks(pure_Liver(:,i));
% process each pair here before going to next set
...
If you need to save the intermediate results, use cell arrays to save the returned peaks/locations of each type
  댓글 수: 3
Image Analyst
Image Analyst 2021년 5월 1일
In your screenshot, the workspace shown applies to untitled4.m, which is not shown.
The workspace does not apply to the test.m that is shown. It appears you messed up somehow while trying to show us what you were doing.
And it appears that you've somehow mistakenly removed the 'Pure Brain Spectra.csv' that I'm sure you posted earlier. Please attach it again.
Phudit Kanittasut
Phudit Kanittasut 2021년 5월 1일
clear
Data = readmatrix('Pure Brain Spectra.csv');
rows=length(Data)
cols=width(Data)
for i = 2:cols
hold on
Xlocs = Data(:,1);
Ylocs = Data(:,i);
select = [Xlocs Ylocs]
plot(Data(:,1),Data(:,i));
[pks,locsLiv] = findpeaks(select(:,i));
hold off
end
This is my code and I just want to find peak from each spectrum from this data This first column is X coordinate and the rest are Y coordinate for each spectrum

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2021년 5월 1일
There were so many errors in your code that I can't explain them all. All I'm going to do is show you how it's done.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
Data = readmatrix('Pure Brain Spectra.csv');
whos Data
[rows, columns] = size(Data)
Xlocs = Data(:, 1);
for col = 2 : columns
thisColumnY = Data(:, col);
nexttile;
plot(Xlocs, Data(:, col), 'b-');
[peakValues, indexesOfPeaks] = findpeaks(thisColumnY, 'Threshold', 1000);
grid on;
hold on
plot(Xlocs(indexesOfPeaks), peakValues, 'rv', 'LineWidth', 2, 'MarkerSize', 7);
caption = sprintf('Column %d', col);
title(caption, 'FontSize', fontSize);
drawnow;
end
hold off;
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m ...\n', mfilename);
but you should really take time to understand all the options for findpeaks() because I'm not sure what you consider to be a peak or not and there are just too many of them identified if you simply go with the defaults.

카테고리

Help CenterFile Exchange에서 AI for Signals에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by