# How to calculate peak value and duration of signal in Image

9 views (last 30 days)

Show older comments

Hello,I have the following Image originalimage.png i want to calculate the peak value of the signal and also the duration of the each signal, as i attached the image parameterinstersted.png in which i describes which values i am currently determing

How can i do it in MATLAB

### Accepted Answer

Image Analyst
on 26 Oct 2022

Try this:

% Demo by Image Analyst

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 = 22;

markerSize = 40;

%--------------------------------------------------------------------------------------------------------

% READ IN IMAGE

folder = [];

baseFileName = 'OriginalImage.png';

fullFileName = fullfile(folder, baseFileName);

% Check if file exists.

if ~exist(fullFileName, 'file')

% The file doesn't exist -- didn't find it there in that folder.

% Check the entire search path (other folders) for the file by stripping off the folder.

fullFileNameOnSearchPath = baseFileName; % No path this time.

if ~exist(fullFileNameOnSearchPath, 'file')

% Still didn't find it. Alert user.

errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);

uiwait(warndlg(errorMessage));

return;

end

end

grayImage = imread(fullFileName);

% Get the dimensions of the image.

% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.

[rows, columns, numberOfColorChannels] = size(grayImage)

%--------------------------------------------------------------------------------------------------------

% Display the image.

subplot(2, 2, 1);

imshow(grayImage);

impixelinfo;

axis('on', 'image');

title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');

if numberOfColorChannels > 1

% It's not really gray scale like we expected - it's color.

fprintf('It is not really gray scale like we expected - it is color\n');

% Extract the blue channel.

grayImage = grayImage(:, :, 3);

end

% Update the dimensions of the image.

% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.

[rows, columns, numberOfColorChannels] = size(grayImage)

% Maximize window.

g = gcf;

g.WindowState = 'maximized';

drawnow;

%--------------------------------------------------------------------------------------------------------

% Threshold the image to get the bright blobs.

mask = grayImage > 0;

subplot(2, 2, 2);

imshow(mask)

title('Binary Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');

% Get the top row for each column

topRows = nan(1, columns);

for col = 1 : columns

thisColumn = mask(:, col);

% Find top row

t = find(thisColumn, 1, 'first');

if ~isempty(t)

topRows(col) = t;

end

end

% Now overlay it onto the image

subplot(2, 2, 3);

imshow(grayImage)

hold on;

plot(topRows, 'r-', 'LineWidth', 4)

title('Top Row in Red', 'FontSize', fontSize, 'Interpreter', 'None');

% Invert it and plot as a line plot

topRows = rows - topRows;

subplot(2, 2, 4);

plot(topRows, 'b-', 'LineWidth', 3);

grid on;

title('Line Plot', 'FontSize', fontSize, 'Interpreter', 'None');

##### 13 Comments

Image Analyst
on 1 Nov 2022

### More Answers (2)

KALYAN ACHARJYA
on 26 Oct 2022

There are many threds sillimar quaestions, calculate the FWHM and time duration of the signal, please refer

Hope it Helps!

##### 2 Comments

KALYAN ACHARJYA
on 26 Oct 2022

Star Strider
on 26 Oct 2022

The indexing in the image makes this a challenge, since they reflect back and in any event are non-monotonic, eliminating a straightforward solution and requiring a more intensive approach. The two peaks appear to be mirror images of each other, so I only analysed the left one here.

The Full-Width-Half-Maximum Value is 102.5 index units.

Try this —

Img = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1169303/OriginalImage.png');

CC = bwconncomp(Img,8);

PIL = CC.PixelIdxList;

[yc,xc] = cellfun(@(x)ind2sub(CC.ImageSize,x), PIL, 'Unif',0);

xy = [cell2mat(xc.') cell2mat(yc.')];

xy(:,2) = max(xy(:,2)) - xy(:,2);

xyh = xy(1:fix(size(xy,1)/2),:);

figure

plot(1:size(xyh,1), xyh(:,1), 'DisplayName','Row Index')

hold on

plot(1:size(xyh,1), xyh(:,2), 'DisplayName','Column Index')

hold off

grid

legend('Location','best')

figure

plot((250:350), xyh(250:350,1), 'DisplayName','Row Index')

hold on

plot((250:350), xyh(250:350,2), 'DisplayName','Column Index')

hold off

grid

title('Original Vectors')

legend('Location','best')

figure

plot(xyh(:,1), xyh(:,2))

grid

xlabel('Row Index')

ylabel('Column Index)')

[maxv,ixh] = max(xyh(:,2));

xv1 = 1:ixh; % Lower Section Index Vector

xv2 = ixh+1:size(xyh,1); % Upper Section Index Vector

figure

plot(xyh(xv1,1), xyh(xv1,2), 'DisplayName','Lower Section')

hold on

plot(xyh(xv2,1), xyh(xv2,2), 'DisplayName','Upper Section')

hold off

grid

xlabel('Row Index')

ylabel('Column Index)')

legend('Location','best')

Lv1 = diff([0; xyh(xv1,2)]) >= 1; % Select Indices To Elliminate Discontinuities

Lv2 = diff([0; xyh(xv2,2)]) <= 1; % Select Indices To Elliminate Discontinuities

figure

plot(xv1(Lv1(250:350)), xyh(xv1(Lv1(250:350)),1), 'DisplayName','Selected Row Index')

hold on

plot(xv1(Lv1(250:350)), xyh(xv1(Lv1(250:350)),2), 'DisplayName','Selected Column Index')

hold off

grid

title('Selected-Element Vectors')

xlabel('Absolute Index')

legend('Location','best')

ixv1 = xv1(Lv1); % Selected Indices

ixv2 = xv2(Lv2); % Selected Indices

yq = max(xyh(:,2))/2

ix1 = find(diff(sign(xyh(ixv1,2)-yq)))+[0 1]; % Approximate Index

xq(1) = interp1(xyh(ixv1(ix1),2), xyh(ixv1(ix1),1), yq); % Interpolate

ix2 = find(diff(sign(xyh(ixv2,2)-yq)))+[0 1]; % Approximate Index

xq(2) = interp1(xyh(ixv2(ix2),2), xyh(ixv2(ix2),1), yq); % Interpolate

FWHM = xq(2)- xq(1) % FWHM: Desired Result (Units: Index)

figure

plot(xyh(ixv1,1), xyh(ixv1,2))

hold on

plot(xyh(ixv2,1), xyh(ixv2,2))

hold off

grid

xlabel('Row Index')

ylabel('Column Index)')

.

##### 2 Comments

Star Strider
on 28 Oct 2022

The ‘102.5’ value is the full-width-half-maximum (FWHM) value of the peak. (Because of the nature of the data, it is extremely difficult to draw it on the plot.) This is typically what is referred to as the ‘width’ of a peak Since you didn’t definie ‘width’, I used that value, since FWHM is the most commonly-used metric.

It may be difficult to determine with any accuracy where a peak begins and ends, as it is here as well, if you look at the data. There are several consecutive zero values at both minima near 250 and 425. Does the peak begin at the position of the first zero or the last in the blue portion of the curve, and similarly does it end at the first or last zero in the red pottion of the curve? The FWHM value is much easier to determine.

It calculates the maximum as ‘999’, the result of the max function, in order to get the ‘yq’ (half-maximum) value. It just doesn’t specifically report it.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!