For each peak, I would like to calculate the delta (shown in image) for the plot in the script at each threshold level in a table.

15 views (last 30 days)
Rufus Adjetey on 1 Feb 2023
Commented: Star Strider on 3 Feb 2023
%Question Continued: I need a sum of all deltas at each threshold level in
%a 7 x 2 table . I tried this code but it gives me an error. Do not know
%where the fault is.
Thresholds = 30:20:150;
X = 0:0.01:1;
Strain = [36 63 73 92 184 45 157 123]';
Threshold = 1:0.5:4;
Y = Strain.*sin(pi*X);
figure
plot(X,Y)
yline(Thresholds,'-r')
ylabel('Strain')
xlabel('time(s)')
X = X(:).'; % Data Must Be Row Vectors
Y = Y(:).'; % Data Must Be Row Vectors
[pks,plocs] = findpeaks(Y);
[vys,vlocs] = findpeaks(-Y);
vlocs = unique([1 vlocs numel(Y)]);
for i = 1:numel(vlocs)-1
idx = vlocs(i) : vlocs(i+1);
[Width(i,:),Results{i}] = interpStrain(X(idx), Y(idx), Thresholds); % Calculates the widths based on function interStrain.
Strain_Width_Table = Results{i};
end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Index exceeds the number of array elements. Index must not exceed 101.
function [Width,TW] = interpStrain(X,Y,Thresholds)
Thresholds = Thresholds(:);
Xend = numel(X);
for i = 1:numel(Thresholds)
fprintf(repmat('-',1,20))
idx1 = find(diff(sign(Y-Thresholds(i))));
idx = [min(idx1) max(idx1)];
if ~isempty(idx)
for j = 1:numel(idx)
idxrng = max(1,idx(j)-1) : min(Xend,idx(j)+1);
xval(i,j) = interp1(Y(idxrng), X(idxrng), Thresholds(i));
end
else
xval(i,:) = [0 0];
end
Width(i,:) = diff(xval(i,:));
end
% Final = Width';
TW = table(Thresholds,Width);
end

Star Strider on 2 Feb 2023
Try my code —
X = [0.0318400000000000,0.0368000000000000,0.0417600000000000,0.0467200000000000,0.0516800000000000,0.0566400000000000,0.0616000000000000,0.101280000000000,0.106240000000000,0.111200000000000,0.116160000000000,0.121120000000000,0.126080000000000,0.165760000000000,0.170720000000000,0.175680000000000,0.180640000000000,0.185600000000000,0.190560000000000,0.195520000000000,0.230240000000000,0.235200000000000,0.240160000000000,0.245120000000000,0.250080000000000,0.255040000000000,0.260000000000000,0.264960000000000,0.299680000000000,0.304640000000000,0.309600000000000,0.314560000000000,0.319520000000000,0.324480000000000,0.329440000000000,0.364160000000000,0.369120000000000,0.374080000000000,0.379040000000000,0.384000000000000,0.388960000000000,0.393920000000000,0.433600000000000,0.438560000000000,0.443520000000000,0.448480000000000,0.453440000000000,0.458400000000000,0.498080000000000,0.503040000000000,0.508000000000000,0.512960000000000,0.517920000000000,0.522880000000000,0.527840000000000,0.567520000000000,0.572480000000000,0.577440000000000,0.582400000000000,0.587360000000000,0.592320000000000,0.632000000000000,0.636960000000000,0.641920000000000,0.646880000000000,0.651840000000000,0.656800000000000,0.661760000000000,0.696480000000000,0.701440000000000,0.706400000000000,0.711360000000000,0.716320000000000,0.721280000000000,0.726240000000000,0.765920000000000,0.770880000000000,0.775840000000000,0.780800000000000,0.785760000000000,0.790720000000000,0.830400000000000,0.835360000000000,0.840320000000000,0.845280000000000,0.850240000000000,0.855200000000000,0.860160000000000,0.899840000000000,0.904800000000000,0.909760000000000,0.914720000000000,0.919680000000000,0.924640000000000,0.964320000000000,0.969280000000000,0.974240000000000,0.979200000000000,0.984160000000000,0.989120000000000,0.994080000000000,1.03376000000000,1.03872000000000,1.04368000000000,1.04864000000000,1.05360000000000,1.05856000000000,1.09824000000000,1.10320000000000,1.10816000000000,1.11312000000000,1.11808000000000,1.12304000000000,1.12800000000000,1.16272000000000,1.16768000000000,1.17264000000000,1.17760000000000,1.18256000000000,1.18752000000000,1.19248000000000,1.23216000000000,1.23712000000000,1.24208000000000,1.24704000000000,1.25200000000000,1.25696000000000,1.26192000000000,1.29664000000000,1.30160000000000,1.30656000000000,1.31152000000000,1.31648000000000,1.32144000000000,1.32640000000000,1.36608000000000,1.37104000000000,1.37600000000000,1.38096000000000,1.38592000000000,1.39088000000000,1.43056000000000,1.43552000000000,1.44048000000000,1.44544000000000,1.45040000000000,1.45536000000000,1.46032000000000,1.50000000000000,1.50496000000000,1.50992000000000,1.51488000000000,1.51984000000000,1.52480000000000,1.56448000000000,1.56944000000000,1.57440000000000,1.57936000000000,1.58432000000000,1.58928000000000,1.59424000000000,1.63392000000000,1.63888000000000,1.64384000000000,1.64880000000000,1.65376000000000,1.65872000000000,1.69840000000000,1.70336000000000,1.70832000000000,1.71328000000000,1.71824000000000,1.72320000000000,1.72816000000000,1.76288000000000,1.76784000000000,1.77280000000000,1.77776000000000,1.78272000000000,1.78768000000000,1.79264000000000,1.83232000000000,1.83728000000000,1.84224000000000,1.84720000000000,1.85216000000000,1.85712000000000,1.86208000000000,1.89680000000000,1.90176000000000,1.90672000000000,1.91168000000000,1.91664000000000,1.92160000000000,1.92656000000000,1.96624000000000,1.97120000000000,1.97616000000000,1.98112000000000,1.98608000000000,1.99104000000000,2.03072000000000,2.03568000000000,2.04064000000000,2.04560000000000,2.05056000000000,2.05552000000000,2.06048000000000,2.10016000000000,2.10512000000000,2.11008000000000,2.11504000000000,2.12000000000000,2.12496000000000,2.16464000000000,2.16960000000000,2.17456000000000,2.17952000000000,2.18448000000000,2.18944000000000,2.19440000000000,2.22912000000000,2.23408000000000,2.23904000000000,2.24400000000000,2.24896000000000,2.25392000000000,2.25888000000000,2.29856000000000,2.30352000000000,2.30848000000000,2.31344000000000,2.31840000000000,2.32336000000000,2.32832000000000,2.36304000000000,2.36800000000000,2.37296000000000,2.37792000000000,2.38288000000000,2.38784000000000,2.39280000000000,2.43248000000000,2.43744000000000,2.44240000000000,2.44736000000000,2.45232000000000,2.45728000000000,2.49696000000000,2.50192000000000,2.50688000000000,2.51184000000000,2.51680000000000,2.52176000000000,2.52672000000000,2.56640000000000,2.57136000000000,2.57632000000000,2.58128000000000,2.58624000000000,2.59120000000000,2.63088000000000,2.63584000000000,2.64080000000000,2.64576000000000,2.65072000000000,2.65568000000000,2.66064000000000,2.70032000000000,2.70528000000000,2.71024000000000,2.71520000000000,2.72016000000000,2.72512000000000,2.76480000000000,2.76976000000000,2.77472000000000,2.77968000000000,2.78464000000000,2.78960000000000,2.79456000000000,2.82928000000000,2.83424000000000,2.83920000000000,2.84416000000000,2.84912000000000,2.85408000000000,2.85904000000000,2.89872000000000,2.90368000000000,2.90864000000000,2.91360000000000,2.91856000000000,2.92352000000000,2.92848000000000,2.96320000000000,2.96816000000000,2.97312000000000,2.97808000000000,2.98304000000000,2.98800000000000,2.99296000000000,3.03264000000000,3.03760000000000,3.04256000000000,3.04752000000000,3.05248000000000,3.05744000000000,3.09712000000000,3.10208000000000,3.10704000000000,3.11200000000000,3.11696000000000,3.12192000000000,3.12688000000000,3.16656000000000,3.17152000000000,3.17648000000000,3.18144000000000,3.18640000000000,3.19136000000000,3.23104000000000,3.23600000000000,3.24096000000000,3.24592000000000,3.25088000000000,3.25584000000000,3.26080000000000,3.30048000000000,3.30544000000000,3.31040000000000,3.31536000000000,3.32032000000000,3.32528000000000,3.36496000000000,3.36992000000000,3.37488000000000,3.37984000000000,3.38480000000000,3.38976000000000,3.39472000000000,3.42944000000000,3.43440000000000,3.43936000000000,3.44432000000000,3.44928000000000,3.45424000000000,3.45920000000000,3.49888000000000,3.50384000000000,3.50880000000000,3.51376000000000,3.51872000000000,3.52368000000000,3.52864000000000,3.56336000000000,3.56832000000000,3.57328000000000,3.57824000000000,3.58320000000000,3.58816000000000,3.59312000000000,3.63280000000000,3.63776000000000,3.64272000000000,3.64768000000000,3.65264000000000,3.65760000000000,3.69728000000000,3.70224000000000,3.70720000000000,3.71216000000000,3.71712000000000,3.72208000000000,3.72704000000000,3.76672000000000,3.77168000000000,3.77664000000000,3.78160000000000,3.78656000000000,3.79152000000000,3.83120000000000,3.83616000000000,3.84112000000000,3.84608000000000,3.85104000000000,3.85600000000000,3.86096000000000,3.89568000000000,3.90064000000000,3.90560000000000,3.91056000000000,3.91552000000000,3.92048000000000,3.92544000000000,3.96512000000000,3.97008000000000,3.97504000000000,3.98000000000000,3.98496000000000,3.98992000000000,3.99488000000000,4.02960000000000,4.03456000000000,4.03952000000000,4.04448000000000,4.04944000000000,4.05440000000000,4.05936000000000,4.09904000000000,4.10400000000000,4.10896000000000,4.11392000000000,4.11888000000000,4.12384000000000,4.16352000000000,4.16848000000000,4.17344000000000,4.17840000000000,4.18336000000000,4.18832000000000,4.19328000000000,4.23296000000000,4.23792000000000,4.24288000000000,4.24784000000000,4.25280000000000,4.25776000000000,4.29744000000000,4.30240000000000,4.30736000000000,4.31232000000000,4.31728000000000,4.32224000000000,4.32720000000000,4.36688000000000,4.37184000000000,4.37680000000000,4.38176000000000,4.38672000000000,4.39168000000000,4.43136000000000,4.43632000000000,4.44128000000000,4.44624000000000,4.45120000000000,4.45616000000000,4.46112000000000,4.49584000000000,4.50080000000000,4.50576000000000,4.51072000000000,4.51568000000000,4.52064000000000,4.52560000000000,4.56528000000000,4.57024000000000,4.57520000000000,4.58016000000000,4.58512000000000,4.59008000000000,4.59504000000000,4.62976000000000,4.63472000000000,4.63968000000000,4.64464000000000,4.64960000000000,4.65456000000000,4.65952000000000,4.69920000000000,4.70416000000000,4.70912000000000,4.71408000000000,4.71904000000000,4.72400000000000,4.76368000000000,4.76864000000000,4.77360000000000,4.77856000000000,4.78352000000000,4.78848000000000,4.79344000000000,4.83312000000000,4.83808000000000,4.84304000000000,4.84800000000000,4.85296000000000,4.85792000000000,4.89760000000000,4.90256000000000,4.90752000000000,4.91248000000000,4.91744000000000,4.92240000000000,4.92736000000000,4.96704000000000,4.97200000000000,4.97696000000000,4.98192000000000,4.98688000000000,4.99184000000000,5.03152000000000];
Y = [0.0237235890000000,0.0991239370000000,0.141935999000000,0.138421576000000,0.118613010000000,0.0837882730000000,0.0304329420000000,0.132351209000000,0.284429877000000,0.311906275000000,0.291458723000000,0.244812745000000,0.153757240000000,0.0623822420000000,0.429160206000000,0.599130482000000,0.599449975000000,0.558554871000000,0.467499366000000,0.170051383000000,0.0301134490000000,0.613827160000000,0.979327152000000,1.08060643300000,1.03204349700000,0.928847258000000,0.566542196000000,0.0186117010000000,0.479320607000000,1.02501465100000,1.26878781000000,1.24386735600000,1.15089489300000,0.785394901000000,0.130434251000000,0.102318867000000,0.879964829000000,1.36207976600000,1.44642591800000,1.37134506300000,1.10360992900000,0.463345957000000,0.728844640000000,1.42821481700000,1.69467197900000,1.64355309900000,1.46208107500000,0.874213955000000,0.389543074000000,1.36559418900000,1.85250152100000,1.88413132800000,1.77582320100000,1.30201508200000,0.368456536000000,1.09785905500000,1.85761340900000,2.08668989000000,2.00010728700000,1.68444820300000,0.862392714000000,0.694019903000000,1.71863395400000,2.20873621600000,2.20745824400000,2.02854216400000,1.35888483600000,0.186345526000000,0.205834599000000,1.40872574400000,2.18125981800000,2.38669381700000,2.27519076000000,1.79914619000000,0.760793940000000,0.953128726000000,2.00330221700000,2.48765360500000,2.44420255700000,2.17039705600000,1.31319733700000,0.399127864000000,1.67102949700000,2.42183804700000,2.56369293900000,2.43653472500000,1.81735729100000,0.598810989000000,1.17805179800000,2.20713875100000,2.64644162600000,2.57391671500000,2.20330483500000,1.19498492700000,0.550567546000000,1.86240580400000,2.57934809600000,2.66081881100000,2.48509766100000,1.74770781700000,0.419894909000000,1.35505092000000,2.34995212200000,2.71928603000000,2.62567458100000,2.15953429400000,1.05153257000000,0.776449097000000,2.02822267100000,2.66784765700000,2.69724101300000,2.46337213700000,1.62885642100000,0.239061871000000,0.146728394000000,1.53013308400000,2.45762126300000,2.75059634400000,2.63973227300000,2.08860684800000,0.892105563000000,0.956323656000000,2.15953429400000,2.72855132700000,2.71800805800000,2.42726942800000,1.49914226300000,0.0655771720000000,0.342577603000000,1.70010336000000,2.56560989700000,2.77104389600000,2.64356618900000,2.00234373800000,0.731081091000000,1.12916936900000,2.27135684400000,2.78190665800000,2.72631487600000,2.36496829300000,1.35441193400000,0.483793509000000,1.85761340900000,2.64292720300000,2.77391933300000,2.61193638200000,1.88477031400000,0.546733630000000,1.29434725000000,2.35602248900000,2.79564485700000,2.71800805800000,2.27710771800000,1.18635861600000,0.640984065000000,1.98125720000000,2.70363087300000,2.77455831900000,2.56209547400000,1.74738832400000,0.361747183000000,1.44674541100000,2.44707799400000,2.81705088800000,2.71641059300000,2.19180308700000,1.02309769300000,0.848974008000000,2.11736121800000,2.75379127400000,2.77232186800000,2.50490622700000,1.61511822200000,0.186026033000000,0.211265980000000,1.61671568700000,2.53589704800000,2.81737038100000,2.70075543600000,2.09627468000000,0.856002854000000,1.03172400400000,2.23493464200000,2.79915928000000,2.77615578400000,2.45314836100000,1.47422180900000,0.0157362640000000,0.390501553000000,1.77294776400000,2.63334241300000,2.81928733900000,2.67423751700000,1.98924452500000,0.687949536000000,1.21319602800000,2.34739617800000,2.83653996100000,2.77232186800000,2.37742852000000,1.31447530900000,0.528842022000000,1.94195956100000,2.72887082000000,2.84548576500000,2.66337475500000,1.87742197500000,0.505838526000000,1.36207976600000,2.45346785400000,2.88222746000000,2.79628384300000,2.30522310200000,1.15217286500000,0.724691231000000,2.08093901600000,2.80810508400000,2.86337737300000,2.62503559500000,1.74962477500000,0.324366502000000,0.0617432560000000,1.53907888800000,2.54164792200000,2.90299450500000,2.79883978700000,2.22215492200000,0.984758533000000,0.917345510000000,2.21065317400000,2.85379258300000,2.86114092200000,2.56784634800000,1.60776988300000,0.142255492000000,0.278040017000000,1.69339400700000,2.62631356700000,2.88286644600000,2.76593200800000,2.11001287900000,0.808078904000000,1.07996744700000,2.29340186100000,2.86816976800000,2.83685945400000,2.47902729400000,1.44035555100000,0.420853388000000,1.84036078700000,2.70650631000000,2.87967151600000,2.73046828500000,1.98828604600000,0.633955219000000,1.26399541500000,2.38733280300000,2.88733934800000,2.81737038100000,2.38893026800000,1.27805310700000,0.568459154000000,1.99467590600000,2.76976592400000,2.87104520500000,2.67583498200000,1.85537695800000,0.451205223000000,1.42054698500000,2.47870780100000,2.89820211000000,2.80778559100000,2.28605352200000,1.11095826800000,0.792104254000000,2.13046043100000,2.82631618500000,2.86657230300000,2.61896522800000,1.71416105200000,0.277081538000000,0.125322363000000,1.59179523300000,2.57807012400000,2.91321828100000,2.80554914000000,2.20106838400000,0.942904950000000,0.966227939000000,2.24867284100000,2.86273838700000,2.85858497800000,2.54484285200000,1.56208238400000,0.0863442170000000,0.323727516000000,1.74706883100000,2.66593069900000,2.90075805400000,2.77232186800000,2.08732887600000,0.772615181000000,1.14514401900000,2.33525544400000,2.88957579900000,2.84708323000000,2.46944250400000,1.39882146100000,0.470374803000000,1.88860423000000,2.73845561000000,2.89372920800000,2.72855132700000,1.95090536500000,0.592421129000000,1.31511429500000,2.43174233000000,2.91098183000000,2.83366452400000,2.37838699900000,1.23588003100000,0.635872177000000,2.03461253100000,2.79596435000000,2.87903253000000,2.66912562900000,1.81895475600000,0.397849892000000,1.47166586500000,2.51576898900000,2.91769118300000,2.81800936700000,2.27359329500000,1.07166062900000,0.848335022000000,2.16496567500000,2.84995866700000,2.87551810700000,2.60394905700000,1.67134899000000,0.215738882000000,0.189540456000000,1.64163614100000,2.60426855000000,2.91769118300000,2.80522964700000,2.17518945100000,0.899453902000000,1.02373667900000,2.28253909900000,2.88638086900000,2.86944774000000,2.53525806200000,1.52885511200000,0.0342668580000000,0.359830225000000,1.78988089300000,2.68861470200000,2.88797833400000,2.75634721800000,2.05474059000000,0.715745427000000,1.20041630800000,2.36560727900000,2.90331399800000,2.85059765300000,2.44739748700000,1.35888483600000,0.505199540000000,1.94419601200000,2.75986164100000,2.89436819400000,2.71704957900000,1.92534592500000,0.547372616000000,1.35920432900000,2.46081619300000,2.91321828100000,2.83238655200000,2.35474451700000,1.19051202500000,0.698173312000000,2.07231270500000,2.81609240900000,2.88701985500000,2.65890185300000,1.78892241400000,0.355037830000000,0.0186117010000000,1.52502119600000,2.54707930300000,2.92280307100000,2.81832886000000,2.25090929200000,1.02597313000000,0.901051367000000,2.20809723000000,2.85251461100000,2.86912824700000,2.58733542100000,1.63428780200000,0.160786086000000,0.239700857000000,1.67486341300000,2.62695255300000,2.90555044900000,2.78893550400000,2.13940623500000,0.838750232000000,1.06399279700000,2.30554259500000,2.88957579900000,2.86369686600000,2.51576898900000,1.48284812000000,0.400405836000000,1.83908281500000,2.71449363500000,2.90299450500000,2.75219380900000,2.02215230400000,0.672294379000000,1.25153518800000,2.38637432400000,2.91066233700000,2.84644424400000,2.42758892100000,1.32310162000000,0.547692109000000,1.99180046900000,2.78446260200000,2.89756312400000,2.69883847800000,1.89307713200000,0.494017285000000,1.41671306900000,2.49691890200000,2.92376155000000,2.83749844000000,2.33493595100000,1.16367461300000,0.762071912000000,2.12087564100000,2.83909590500000,2.89053427800000,2.65091452800000,1.75793159300000,0.311267289000000,0.0802738500000000,1.57614007600000,2.57902860300000,2.92951242400000,2.82248226900000,2.23205920500000,0.982841575000000,0.947377852000000,2.24164399500000,2.86880875400000,2.86753078200000,2.57072178500000,1.60489444600000,0.123085912000000,0.299765541000000,1.73365012500000,2.66241627600000,2.92024712700000,2.79149144800000,2.11672223200000,0.800411072000000,1.12246001600000,2.34196479700000,2.89724363100000,2.85954345700000,2.49596042300000,1.44706490400000,0.443217898000000,1.88892372300000,2.73781662400000,2.90331399800000,2.74197003300000,1.99371742700000,0.631399275000000,1.29402775700000,2.42247703300000,2.92024712700000,2.85123663900000,2.40394643900000,1.28124803700000,0.603603384000000,2.03173709400000,2.80203471700000,2.89564616600000,2.68605875800000,1.85729391600000,0.446093335000000,1.46016411700000,2.52567327200000,2.91673270400000,2.82376024100000,2.29627729800000,1.11127776100000,0.818941666000000];
Strain = 1:0.5:2.5;
[pks,plocs] = findpeaks(Y);
[vys,vlocs] = findpeaks(-Y);
vlocs = [1 vlocs numel(Y)];
for k1 = 1:numel(vlocs)-1
idx = vlocs(k1) : vlocs(k1+1);
[Width(k1,:),Results{k1}] = interpStrain(X(idx), Y(idx), Strain, k1);
% figure
% plot(X(idx), Y(idx))
% yline(Strain, 'Color', 'r', 'LineWidth', 1);
% text(X(plocs(k1))*ones(size(Strain)),Strain, compose('\\Delta t = %.5f',Width(k1,:)), 'Horiz','center', 'Vert','middle', 'FontSize',9)
% xlabel('No. of Cycles');
% ylabel('Voltage [V]');
% title(sprintf('Peak #%3d',k1))
%
% fprintf('\nPeak #%3d —\n',k1)
Strain_Width_Table = Results{k1};
end
Cvert = cat(2,Results{:}) % Horizontally Copncatenate The Tables
Cvert = 4×75 table
Width_Peak_001 Width_Peak_002 Width_Peak_003 Width_Peak_004 Width_Peak_005 Width_Peak_006 Width_Peak_007 Width_Peak_008 Width_Peak_009 Width_Peak_010 Width_Peak_011 Width_Peak_012 Width_Peak_013 Width_Peak_014 Width_Peak_015 Width_Peak_016 Width_Peak_017 Width_Peak_018 Width_Peak_019 Width_Peak_020 Width_Peak_021 Width_Peak_022 Width_Peak_023 Width_Peak_024 Width_Peak_025 Width_Peak_026 Width_Peak_027 Width_Peak_028 Width_Peak_029 Width_Peak_030 Width_Peak_031 Width_Peak_032 Width_Peak_033 Width_Peak_034 Width_Peak_035 Width_Peak_036 Width_Peak_037 Width_Peak_038 Width_Peak_039 Width_Peak_040 Width_Peak_041 Width_Peak_042 Width_Peak_043 Width_Peak_044 Width_Peak_045 Width_Peak_046 Width_Peak_047 Width_Peak_048 Width_Peak_049 Width_Peak_050 Width_Peak_051 Width_Peak_052 Width_Peak_053 Width_Peak_054 Width_Peak_055 Width_Peak_056 Width_Peak_057 Width_Peak_058 Width_Peak_059 Width_Peak_060 Width_Peak_061 Width_Peak_062 Width_Peak_063 Width_Peak_064 Width_Peak_065 Width_Peak_066 Width_Peak_067 Width_Peak_068 Width_Peak_069 Width_Peak_070 Width_Peak_071 Width_Peak_072 Width_Peak_073 Width_Peak_074 Width_Peak_075 ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ ______________ 1.0 0 0 0 0.010448 0.017155 0.019408 0.021816 0.023302 0.029293 0.024837 0.025343 0.038175 0.025784 0.049003 0.025894 0.047299 0.026159 0.026253 0.026347 0.026309 0.053828 0.026216 0.053982 0.026147 0.046402 0.026344 0.026401 0.033576 0.026384 0.056789 0.026319 0.055705 0.026402 0.026549 0.02654 0.026499 0.05361 0.026396 0.056975 0.026321 0.055823 0.026494 0.026577 0.026559 0.026546 0.057305 0.02642 0.057701 0.026367 0.054962 0.026516 0.026601 0.034134 0.026487 0.057889 0.026425 0.057711 0.02644 0.034779 0.02656 0.026517 0.053774 0.026489 0.058565 0.026413 0.058892 0.02653 0.026642 0.026623 0.026586 0.05756 0.026508 0.058876 0.026436 0.05791 1.5 0 0 0 0 0 0 0.012507 0.016398 0.018328 0.019853 0.020683 0.021136 0.021799 0.021748 0.022136 0.02207 0.022393 0.022388 0.022554 0.022531 0.022475 0.022557 0.022413 0.022507 0.022512 0.022654 0.022636 0.022739 0.022684 0.02268 0.022756 0.022677 0.022833 0.022866 0.02293 0.022842 0.022798 0.022818 0.022767 0.022816 0.022785 0.022916 0.022914 0.022944 0.022917 0.022852 0.022845 0.022832 0.022849 0.022898 0.022929 0.022949 0.023019 0.0229 0.022882 0.022902 0.022848 0.022905 0.022963 0.022979 0.022883 0.022976 0.022928 0.02292 0.022923 0.022993 0.022991 0.022999 0.023033 0.022972 0.023014 0.022972 0.022928 0.022943 0.022988 2.0 0 0 0 0 0 0 0 0 0.0068387 0.012244 0.013951 0.015882 0.016204 0.016878 0.017191 0.017339 0.017746 0.017695 0.017821 0.018131 0.01785 0.018155 0.017803 0.018173 0.017911 0.018153 0.018157 0.018144 0.018453 0.01816 0.018701 0.018254 0.018717 0.01845 0.018622 0.018629 0.018367 0.018848 0.018327 0.018932 0.018331 0.018757 0.01858 0.018592 0.018804 0.018452 0.018877 0.01844 0.018906 0.018459 0.018714 0.018675 0.018631 0.018883 0.018475 0.019033 0.018466 0.018898 0.018551 0.018726 0.018678 0.018577 0.01901 0.018489 0.019131 0.01858 0.018937 0.018665 0.018737 0.018857 0.018616 0.019149 0.018522 0.019096 0.018567 2.5 0 0 0 0 0 0 0 0 0 0 0 0 0.0047115 0.0076027 0.0089005 0.0092422 0.0094446 0.01046 0.010671 0.011407 0.010805 0.011586 0.010748 0.011696 0.011257 0.011925 0.011761 0.01183 0.01195 0.011793 0.012394 0.012374 0.01273 0.012696 0.012999 0.012603 0.012806 0.012643 0.012477 0.012709 0.012594 0.012898 0.01282 0.013076 0.012788 0.013005 0.012769 0.012849 0.012835 0.012892 0.013007 0.01286 0.013268 0.012773 0.012993 0.01286 0.012942 0.012934 0.012931 0.013088 0.012788 0.013304 0.012849 0.012948 0.012924 0.013215 0.0131 0.01302 0.0132 0.01293 0.013413 0.012914 0.013042 0.012978 0.013084
RN = Cvert.Properties.RowNames; % Get 'RowNames'
SD = sum(table2array(Cvert),2); % Sum The Widths AT Each Strain Value
Sum_Deltas_Table = table(str2double(RN),SD, 'VariableNames',{'Strain','Sum_Deltas'}) % Create 'table' Of Summed Widths At Each Strain Value
Sum_Deltas_Table = 4×2 table
Strain Sum_Deltas ______ __________ 1 2.5028 1.5 1.543 2 1.2096 2.5 0.76811
figure
bar(Sum_Deltas_Table.Strain, Sum_Deltas_Table.Sum_Deltas)
grid
xlabel('Strain')
ylabel('\Sigma\Delta(t)')
% % Create scatter
% plot(X,Y,'DisplayName','Voltage',...
% 'Marker','NONE');
% % Create ylabel
% ylabel('Voltage [V]');
% xlabel('No. of Cycles');
%
%
%
% % Find the Given threshold Voltage
% yline(Strain, 'Color', 'r', 'LineWidth', 1);
function [Width,TW] = interpStrain(X,Y,Strain,k)
Strain = Strain(:);
Xend = numel(X);
for k1 = 1:numel(Strain)
% Q = Strain(k1)
idx = find(diff(sign(Y-Strain(k1))));
if ~isempty(idx)
for k2 = 1:numel(idx)
idxrng = max(1,idx(k2)-1) : min(Xend,idx(k2)+1);
xval(k1,k2) = interp1(Y(idxrng), X(idxrng), Strain(k1));
end
else
xval(k1,:) = [0 0];
end
Width(k1,:) = diff(xval(k1,:));
end
TW = table(Width, 'RowNames',compose('%.1f',Strain), 'VariableNames',{sprintf('Width_Peak_%03d',k)});
end
My code works.
.
Star Strider on 3 Feb 2023
As always, my pleasure!

Mathieu NOE on 2 Feb 2023
hello
seems to me the code is fine for one single value of Strain because then X and Y have same size
as soon as you have multiple values (array) for Strain and because you are doing this
X = X(:).'; % Data Must Be Row Vectors
Y = Y(:).'; % Data Must Be Row Vectors
then X and Y are anymore similar in size and this is the root cause of your error (Y was 8x101 and now is 1 x 808)
IMO the code must be modified to cope for multiple values of Strain
that's why I added a new for loop and also I think that Width must be a 2D array with number of rows equal to Strain values and number of columns equal to Threshold values
At the end the new table TW looks like this :
TW =
Strain Width_all_results
______ _________________
36 [1×7 double]
63 [1×7 double]
73 [1×7 double]
92 [1×7 double]
184 [1×7 double]
45 [1×7 double]
157 [1×7 double]
123 [1×7 double]
and Width_all_results is a 2D array that contains all values vs Strain and vs Threshold :
Width_all_results =
0.3727 0 0 0 0 0 0
0.6840 0.4163 0 0 0 0 0
0.7304 0.5197 0.1830 0 0 0 0
0.7885 0.6342 0.4495 0.1326 0 0 0
0.8957 0.8248 0.7515 0.6746 0.5920 0.5005 0.3931
0.5354 0 0 0 0 0 0
0.8776 0.7936 0.7058 0.6113 0.5057 0.3789 0.1906
0.8431 0.7335 0.6145 0.4774 0.2952 0 0
Code modified :
Thresholds = 30:20:150;
X = 0:0.01:1;
Strain = [36 63 73 92 184 45 157 123]';
Threshold = 1:0.5:4;
Y = Strain.*sin(pi*X);
figure(1)
plot(X,Y)
for ck = 1:numel(Thresholds) % <= mod (maybe my matlab release is too old, I needed a for loop here)
yline(Thresholds(ck),'-r')
end
ylabel('Strain')
xlabel('time(s)')
% X = X(:).'; % Data Must Be Row Vectors
% Y = Y(:).'; % Data Must Be Row Vectors
Width_all_results = zeros(numel(Strain),numel(Thresholds)); % <= mod
for ck = 1:numel(Strain)
YY = Y(ck,:);
[pks,plocs] = findpeaks(YY);
[vys,vlocs] = findpeaks(-YY);
vlocs = unique([1 vlocs numel(YY)]);
for i = 1:numel(vlocs)-1
idx = vlocs(i) : vlocs(i+1);
[Width(i,:),Results{i}] = interpStrain(X(idx), YY(idx), Thresholds) % Calculates the widths based on function interStrain.
Width_all_results(ck,:) = Width; % <= mod
%Strain_Width_Table = Results{i};
end
end
TW = table(Strain,Width_all_results) % Width_all_results is a 2D array (rows = Strain values , columns = Thresholds values)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Width,TW] = interpStrain(X,Y,Thresholds)
Thresholds = Thresholds(:);
Xend = numel(X);
for i = 1:numel(Thresholds)
fprintf(repmat('-',1,20))
idx1 = find(diff(sign(Y-Thresholds(i))));
idx = [min(idx1) max(idx1)];
if ~isempty(idx)
for j = 1:numel(idx)
idxrng = max(1,idx(j)-1) : min(Xend,idx(j)+1);
xval(i,j) = interp1(Y(idxrng), X(idxrng), Thresholds(i));
end
else
xval(i,:) = [0 0];
end
Width(i,:) = diff(xval(i,:));
end
% Final = Width';
TW = table(Thresholds,Width);
end
Rufus Adjetey on 3 Feb 2023
Thank you. I appreciate this approach too.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by