Calculate time period of a graph signal
19 views (last 30 days)
Show older comments
Bill Kyriakidis
on 27 Mar 2022
Commented: Star Strider
on 27 Mar 2022
Hello guys , i am new to matlab .
I have a signal graph that i want to find the time period of it and the time in which the amplitude gets half of its starting value.
The singal is below :
Any help will be appreciated !Thanks
2 Comments
Accepted Answer
Star Strider
on 27 Mar 2022
Edited: Star Strider
on 27 Mar 2022
Use findpeaks to get the peak indices and then use the associated time vector to calculate the period. Use the envelope function to return the upper envelope of the waveform that can then be used to determine the rate of the decrease in the amplitude.
Alternatively, use the procedure described in Curve Fitting of large Data Measurement? to get the relevant parameters, and use those values to calculate the desired results.
EDIT — (27 Mar 2022 at 19:35)
The envelope turns out not to be a single exponential, so while treating it as such may work, any results derived from that assumption may have some inaccuracies.
There are beginning and trailing parts of the signal that I removed by finding the appropriate indices and the using only that part of the signal for the regression.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/942934/Signal_processing_Bill.txt', 'VariableNamingRule','preserve')
x = linspace(0, numel(T1{:,1})-1, numel(T1{:,1}))';
[pks,locs] = findpeaks(T1{:,1});
Period = mean(diff(x(locs)))
env = envelope(T1{:,1}, 5, 'peak');
figure
plot(x, T1{:,1})
hold on
plot(x,env)
hold off
grid
legend('Signal','Upper Envelope', 'Location','best')
yc(:,1) = T1{:,1};
[mv,ix] = max(yc);
lcs = find(islocalmax(yc(yc(:,1)>0), 'MinProminence',0.05));
lims = [ix max(lcs)];
yc = yc(lims(1):lims(2));
x = linspace(0, numel(yc)-1, numel(yc))';
vn = T1.Properties.VariableNames; % Variable Names
ttlstr = vn(1); % Variable Names Used In The Code
for k = 1:size(yc,2)
yk = yc(:,k);
y = yk;
% y = detrend(y);
ym = mean(y); % Estimate offset
y = y - ym;
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zci = @(v) find(diff(sign(v))); % Returns Approximate Zero-Crossing Indices Of Argument Vector
zt = x(zci(y));
per = 2*mean(diff(zt)) % Estimate period
fit = @(b,x) b(1) .* exp(b(2).*x) .* (sin(2*pi*x./b(3) + 2*pi/b(4))) + b(5); % Objective Function to fit
fcn = @(b) norm(fit(b,x) - yk); % Least-Squares cost function
s0 = [yr; -1E-2; per; -1; ym]; % Initial Parameter Estimates
[s,nmrs] = fminsearch(fcn, s0) % Minimise Least-Squares
xp = linspace(min(x),max(x), 500);
figure
plot(x,yk,':b', 'LineWidth',1.5)
hold on
plot(xp,fit(s,xp), '--r')
hold off
grid
xlabel('Time (s)')
ylabel('Amplitude')
title(ttlstr{k})
legend('Original Data', 'Fitted Curve')
text(0.1*max(xlim),0.7*min(ylim), sprintf('$y = %.3f\\cdot e^{%.3f\\cdot x}\\cdot sin(2\\pi\\cdot x\\cdot %.3f%+.3f) %+.4f$', [s(1:2); 1./s(3:4); s(5)]), 'Interpreter','latex')
end
.
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
