MATLAB: I am using findpeaks (data,locations) func to find time period of trigonometric function. Values(max,location) for only one peak are getting stored, hence time period is coming out to be zero. please help.

findpeaksMATLABtime period

m= 750; % mass of the system in kg
k= 50000; % Stiffness of system in N/m
c= 1000; % damping coefficient Ns/m
time= 0:0.01:1; %time in sec
x_0=0.01; % displacement intial condition
syms x(t)
D1x= diff(x,1); % differentiation
D2x= diff(x,2);
x= dsolve(m*D2x+k*x==0,x(0)==x_0, D1x(0)==x_dot_0,'t');
x_fun=matlabFunction(x) % to save solved function values
x_dot_fun = matlabFunction(diff(x));
[maxima,maxima_location]= findpeaks(x_t,time);
time_period= diff(maxima_location);

Best Answer

  • Dear Abhijeet,
    I am not sure what you mean with "hence time period is coming out to be zero", but I do believe I can be of some assistance:
    If you plot your signal
    plot(time, x_t)
    You will see that there seem to be two peaks, one at time point zero and one at time point 0.77.
    But... If you use findpeaks, the documentation states that it finds local peaks, but only if
    "a data sample that is either larger than its two neighboring samples or is equal to Inf. Non-Inf signal endpoints are excluded."
    I believe in your case it does not come up with the local maxima at time=0, due to that reason (if that is what you mean).