My other question is that let's say I have 5 traces of action potentials again on the same graph. How can I graph a single plot that represents the average of these 5 traces of action potentials? In this case the time that it takes from start of action potential to reach the peak is different for different traces. That means time for one complete cycle of action potential might be different for the other cycle of action potential.
Suppose I have 5 traces of action potential with different peaks. I would like to plot all those 5 traces on the same plot so that all the peaks are same. Basically I would like to plot on the normalized scale.
8 views (last 30 days)
Show older comments
I am trying to do this in matlab.
7 Comments
dpb
on 8 Jun 2017
How big is "very big"? You could certainly attach a .mat file with a third or half one would think without exceeding forum size limits.
Again, expecting anybody to be able to do anything useful just looking at figures is wishful thinking; it'll take somebody figuring out a way to find those breakpoints programmatically and then working out a scaling algorithm.
Again, what would you expect the results to look like if you were to do this by hand? Does it mean anything if align the peaks and what is it that is the actual result supposed to be? We still can only know what you tell us unless there is something about this in open literature that can point to...
Accepted Answer
dpb
on 7 Jun 2017
Edited: dpb
on 9 Jun 2017
If all are same length so have each trace in a column in an array, say data, then
dataNorm=bsxfun(@rdivide, data, max(data));
plot(x,dataNorm)
With data, a start:
[r,lag]=xcorr(Vv1,Vv2); % cross-correlation, save lags
[~,ix]=max(abs(r)); % find maximum
lagDiff=lag(ix); % and the lag number corresponding
figure % see what we started with...
subplot(2,1,1)
plot(Ti1.',[Vv1; Vv2].') % both on same time scale
legend('Vv1','Vv2')
subplot(2,1,2) % and after compensating for lag
plot(Vv1(lagDiff:end)) % the lagged Vv1
hold on
plot(Vv2) % compare to Vv2
legend('lagged Vv1','Vv2') % looks pretty good
delt=mean(Vv1(lagDiff:end)-Vv2(1:end-lagDiff+1)); % what's average offset
VV2=Vv2(1:end-lagDiff+1)+delt; % and adjust Vv2 for that delta
plot(VV2,'cx',markersize',1.5) % and add to see it, too...
Above yields--
Questions are just what is the purpose and what makes sense to do about normalizing magnitudes; what to actually consider if averaging makes sense and all that sorta' thingie...that has more to do with the subject matter under study and what the data mean than just the manipulations--they're pretty straightforward.
You will, it appears, need to do some smoothing to be able to locate peaks reliably in order to separate pulses; findpeaks had difficulty with the raw data as is in that regards.
12 Comments
More Answers (1)
dpb
on 9 Jun 2017
Well, another shot in the dark...putting the two traces you posted before together...
VV=[Vv1 Vv2(2:end)]; T=[Ti1 Ti2(2:end)]; % concatenate data w/o overlap
subplot(2,1,1), plot(T,VV) % show it..
xlim([34 37])
subplot(2,1,2) % look at extents of sizable peaks
findpeaks(VV,T,'minPeakProminence',20,'Annotate','extents')
xlim([34 37]), ylim([0 40])
The results--
>> [pks,locs,widths,amps]=findpeaks(VV,T,'minPeakProminence',20,'Annotate','extents')
pks =
37.1521 32.7119
locs =
34.3990 35.8647
widths =
0.4046 0.4049
amps =
27.4835 27.4677
>> mean([amps.' widths.'])
ans =
27.4756 0.4048
>>
You can try something of the sort on the whole trace and see what happens...
3 Comments
dpb
on 9 Jun 2017
Edited: dpb
on 10 Jun 2017
Read the documentation, look at the plot. The width and prominence are shown by the gold lines on the plot. The definition of prominence and width are given there in terms of how they're calculated; it's too involved to try to repeat here in brief.
As I said in first response, to try to find the actual pulses, I'd use findpeaks on the negative of the trace--
findpeaks(VV,T,'minpeakdistance',1)
[vlys,vlocs]=findpeaks(-VV,T,'minpeakdistance',1);
hold on
scatter(vlocs,abs(vlys),50,'r^','filled')
legend('Trace','Peaks','Valleys')
results in
How robust this will be in general, no klew...
ADDENDUM
Finding "the start of the pulse" again depends on what is the definition of start---the inflection point on the leading edge is going to be much more difficult undoubtedly, if that's it, because there's no real definitive transition; it's smooth transition albeit there is a very sharp positive gradient that might be able to localize...
dpb
on 11 Jun 2017
NB: when look at the detail of the rise time and overlay the gradient the peak of the gradient, while looking like is dead-on in the wider view is, as expected, in the middle of the actual transition zone--it gets you in the neighborhood ok, but there's still a question of just what defines "the pulse", precisely.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!