Borrar filtros
Borrar filtros

Finding peak width at threshold

18 visualizaciones (últimos 30 días)
Poonam Thakur
Poonam Thakur el 14 de Nov. de 2018
Editada: dpb el 16 de Nov. de 2018
I have data with two vectors- one vector corresponds to time and other corresponds to voltages. I found the peaks and corresponding time indices of my data using findpeaks function. However, I want to find the peak widths at thresholds (handdrawn by a blackline in the 2nd picture below) rather than half height or halfprominence(appearing in yellow line). Best would be if I can also see the widths on the graph similar to default findpeaks.
While I can find the time indices corresponding to thresholds(which is an array I obtain from another piece of code, not given here) and subtract them from time indices corresponding to maxvol ( my peak value) to obtain the width of rising part of peak, I cannot think of any logic to find the time index in the dropping down part of the peak, where it would again cross the value equal to threshold voltage.
I am rather new to matlab, so have not been able to find a way to do this. Can anyone help me out.
%%
%1. concatenate all the traces
recording = load(sprintf('LSN16-021118-11f%d.mat'));%load data into struct
fields = fieldnames(recording);%get field names of struct
for k = 1:numel(fields); % numel function returns the number of elements in this array
ind_= sscanf(fields{k},'Trace_%i_%i_%i_%i');%disentangle name into numbers
ind(k,:) = ind_';%save indices in long list
end
relind = ind(:,3);%relevant index is in 3rd column and can change for recordings
[sortind, order] = sort(relind);%sort
CC = [];%initialize a new list
for kk = 1:length(order)
k=order(kk);
CC = cat(1,CC,recording.(fields{order(k)}));%concat, CC is current clamp
end
%%
% 2. Plot the traces
spk.time = CC (:,1);
spk.vol = CC (:,2);
spk.sti= CC (:,3);
spk;
figure(1)
plot (spk.time, spk.vol, 'black')
xlabel 'Time(sec)'
ylabel 'Voltage (V)'
% 3. find peaks in traces
trace_length=10; % 10 seconds
Fs = length(CC)./trace_length;% Fs= sampling frequency
findpeaks(spk.vol,'minpeakheight',0,'Annotate','extents');
title('Signal Peak Widths')
[maxvol,locs_maxvol,width,p]=findpeaks(spk.vol,'minpeakheight',0);
thresholdvoltage= -0.03;
APheight= maxvol-(thresholdvoltage);
meanAPheight= mean(APheight)*1000
spike_width = mean(width),
figure(2)
plot(spk.time, spk.vol, 'black',(locs_maxvol./Fs),maxvol,'o')
  6 comentarios
dpb
dpb el 14 de Nov. de 2018
OK, yes, that's much better...thanks! :) For figures, you can use the funny blue-image icon in the 'INSERT' section and insert the .jpg inline instead of as an attachment and that is often handy to have the picture in context...
Let me have a little while and I'll see if can't get the previous to work with your data; it's very clean-looking it appears.
The problem is to return a width at a user-provided level, correct?
Poonam Thakur
Poonam Thakur el 14 de Nov. de 2018
Yes, I do not have any noise issue, great peak to noise ratio. Just looking to find width at user given level.

Iniciar sesión para comentar.

Respuestas (2)

Akira Agata
Akira Agata el 15 de Nov. de 2018
Editada: Akira Agata el 15 de Nov. de 2018
One simple way to do your task woudl be utilizing pulsewidth function. By setting 'StateLevels' option to [th - delta, th + delta], where th is your given threshold value and delta is some value.
For example, when setting th = 0.0 and delta = 0.02, the pulse width at y = 0.0 can be calculated as follows (sorry for some Japanese language at the figure due to my MATLAB settings...):
load('LSN16-021118-11f.mat');
t = Trace_1_2_1_1(:,1);
y = Trace_1_2_1_1(:,2);
pulsewidth(y,t,'StateLevels',[-0.02 0.02])
To obtain pulse width value for each pulse, please set the output variable, like:
pWidth = pulsewidth(y,t,'StateLevels',[-0.02 0.02]);
  6 comentarios
Poonam Thakur
Poonam Thakur el 16 de Nov. de 2018
Thanks a lot for trying. I couldn't try this solution because I ran into problems with part of the code to calculate thresholds. If I am not able to solve that one, maybe another question is coming your way. :)
dpb
dpb el 16 de Nov. de 2018
Editada: dpb el 16 de Nov. de 2018
What's wrong with the solution I posted as the Answer just below?
This was just a sidebar looking at the pulsewidth function I'd not been aware of before; it's cute but really has no bearing on your particular problem.

Iniciar sesión para comentar.


dpb
dpb el 15 de Nov. de 2018
Editada: dpb el 15 de Nov. de 2018
OK, finally(! :) ) had time to sit down for a few minutes...it's more convoluted than if had just started from scratch, but I thought initially if I picked out the sections from findpeaks that does the locating for the two ways it does that would be quick to modify -- wrong! but, it does work altho could be done quite a lot more simply if want to take the time to streamline it some.
To use as is, use findpeaks to return the peak locations using the array indices as the X values(*), then call the function; it returns a 2D array array of the width low/hi bounds in position units; these can be converted to time by multiplying by the dt or by interpolation for "real" units.
The m-file containing the function and its helpers is attached; ended up long enough owing to using existing structure as base to not paste in its entirety.
load LSN16-021118-11f.mat
x=[1:length(Trace_1_2_1_1(:,1))].'; % x position vector
[pks,locs]=findpeaks(Trace_1_2_1_1(:,2),x,'MinPeakHeight',0.025); % find locations
refHt=-0.030; % desired crossing level
bnds=getpeakwidth(Trace_1_2_1_1(:,2),x,locs,refHt); % find the bounds at level
figure
plot(x,Trace_1_2_1_1(:,2))
for i=1:length(bnds),hL(i)=line(bnds(i,:),[refHt refHt],'color','r');end
xlim([2400 2700])
produced the following figure showing the first peak amplified...
(*) The need for using the index is that one must search the underlying waveform for the crossing so if use the time variable have to convert that internally to be and indes into the array. That's doable with more internal coding, but I took the easy way out and just passed the index vector directly.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by