How can I find when a signal stops oscillating more than a certain value?
14 views (last 30 days)
I have one vector for time and one vector for voltage. I need to find out at what ms in time the voltage (as in the image I attached) stopped oscillating more than a certain value. Moreover, I'd like to be able to also find out the value at which it stabilizes. Is this possible?
Mathieu NOE on 21 Apr 2021
sure it's doable
use the attached code example to do a first derivative of your data, and then find the first index of the data when the abs of the first derivative is below a given threshold ; I used moveman to not be trapped by a single isolated sample where the derivative would be almost zero.
Fs = 10;
dt = 1/Fs;
t = 0:dt:10;
omega = 2*pi*0.25;
tau = 1;
x = sin(omega*t).*(exp(-t/tau)) + (1-exp(-t/tau));
[dxdt, d2xdt2] = firstsecondderivatives(t,x);
dxdtmm = movmean(abs(dxdt),5); % use movmean to discard one isolated time value of low derivative value ; increase window length if needed
threshold = 0.01;
ind = find(dxdtmm < threshold);
t0 = t(ind(1));
x0 = x(ind(1));
% raw data + first derivatives plot
title('Voltage first derivative (abs value)');
disp(['Steady state voltage ' num2str(x0) ' V']);
disp(['Steady state voltage reached at ' num2str(t0) ' s']);
function [dy, ddy] = firstsecondderivatives(x,y)
% The function calculates the first & second derivative of a function that is given by a set
% of points. The first derivatives at the first and last points are calculated by
% the 3 point forward and 3 point backward finite difference scheme respectively.
% The first derivatives at all the other points are calculated by the 2 point
% central approach.
% The second derivatives at the first and last points are calculated by
% the 4 point forward and 4 point backward finite difference scheme respectively.
% The second derivatives at all the other points are calculated by the 3 point
% central approach.
n = length (x);
dy = zeros;
ddy = zeros;
% Input variables:
% x: vector with the x the data points.
% y: vector with the f(x) data points.
% Output variable:
% dy: Vector with first derivative at each point.
% ddy: Vector with second derivative at each point.
dy(1) = (-3*y(1) + 4*y(2) - y(3)) / (2*(x(2) - x(1))); % First derivative
ddy(1) = (2*y(1) - 5*y(2) + 4*y(3) - y(4)) / (x(2) - x(1))^2; % Second derivative
for i = 2:n-1
dy(i) = (y(i+1) - y(i-1)) / (x(i+1) - x(i-1));
ddy(i) = (y(i-1) - 2*y(i) + y(i+1)) / (x(i-1) - x(i))^2;
dy(n) = (y(n-2) - 4*y(n-1) + 3*y(n)) / (2*(x(n) - x(n-1)));
ddy(n) = (-y(n-3) + 4*y(n-2) - 5*y(n-1) + 2*y(n)) / (x(n) - x(n-1))^2;