MATLAB: ways to compute the settling time of a signal in Matlab - matlab

I would like to compute the settling time of a signal y in Matlab. It should give the amount of time required before the signal reaches a steady state error |y(t)-y_{ss}| which is smaller than some absolute value x and stays smaller than x for all future times.
I already tried to use the Matlab function stepinfo, but this defines the value x as "a fraction 2% of their peak value for all future times" and that is not what i want.
Is there any way that i could adjust the Matlab function stepinfo, or a way that i could code this myself?

Related

Fire Alarm detect via MATLAB using FFT

I want to take a wav. file and plot it in Time Domain and Frequency Domain on MATLAB.
I have this code thus far;
[y,fs]=wavread('FireAlarm');
%wavread=the function that is going to read the wav.file
%y=samples
%fs=sampling frequency
%'FireAlarm'=wav file
t=linespace(0,length(y)/fs,length(y));
%linespace=the function that is going to create the time vector
%0=start time
%length(y)/fs=ending time
%length(y)= number of samples in y
plot(t,y)
Nfft=1024;
%Nfft=the length of fft
f=linespace(0,fs,Nfft);
%f= frequency vector
%0=first frequency
%fs=ending frequency
%nfft=length of the frequency vector
G=abs(fft(y,Nfft));
%G= the fft of the samples y in 1024 points
figure;plot(f(1:Nfft/2),G(1:Nfft/2)
Before I can even finish typing the first section, MATLAB tells there is an undefined function or variable 'wavread'.
Anyone know why this may be?

Matlab : Plot of entropy vs digitized code length

[Tent Map][1] is a dynamical system that is discrete in time. Iterations of the map yields a time series.
The entropy of this system when discretized in 0/1 using a threshold = 0.5 is, H = log_2(2) = 0.69 approx. I want to obtain a graph with Y Axis as the entropy and X Axis as the Number of samples or the length of the time series. I have written a code for obatining the entropy by varying the length of the time series. The objective is to see at what length of the discretized time series, I get the entropy H. The code however, loops infinitely and never reaches the entropy value H. Can somebody please help in obtaining the graph? Thank you.
[1]: https://en.wikipedia.org/wiki/Tent_map
You are saying that lambda must be EXACTLY equal to log(2). When the entropy drops below log(2) it skips over this value (for instance instead of exactly log(2) maybe an iteration is 0.69309 which is less). Try replacing the line
while(lambda ~=H)
with
tol=0.01;
while(~(abs(lambda-H)<tol))
this will mean that it quits whenever lambda is close to H (with in the tolerance tol).
If your tol is too small (try 0.001) an iteration would jump over it again and you would be back to the problem you had before.

Matlab: finding phase difference using cpsd

From my understanding, when using the cpsd function as such:
[Pxy,f] = cpsd(x,y,window,Ns,NFFT,Fs);
matlab chops the time series data into smaller windows with size specified by you. And the windows are shifted by Ns data point. The final [Pxy, f] are an average of results obtained from each individual window. Please correct me if I am wrong about this process.
My question is, if I use angle(Pxy) at a specific frequency, say 34Hz. Does that give me the phase difference between signal x and y at the frequency 34Hz?
I am having doubt about this because if Pxy was an average between each individual window, and because each individual was offset by a window shift, doesn't that mean the averaged Pxy's phase is affected by the window shift?
I've tried to correct this by ensuring that the window shift corresponds to an integer of full phase difference corresponding to 34Hz. Is this correct?
And just a little background about what I am doing:
I basically have numerous time-series pressure measurement over 60 seconds at 1000Hz sampling rate.
Power spectrum analysis indicates that there is a peak frequency at 34 Hz for each signal. (averaged over all windows)
I want to compare each signal's phase difference from each other corresponding to the 34Hz peak.
FFT analysis of individual window reveals that this peak frequency moves around. So I am not sure if cpsd is the correct way to be going about this.
I am currently considering trying to use xcorr to calculate the overall time lag between the signals and then calculate the phase difference from that. I have also heard of hilbert transform, but I got no idea how that works yet.
Yes, cpsd works.
You can test your result by set two input signals, such as:
t=[0:0.001:5];
omega=25;
x1=sin(2*pi*omega*t);
x2=sin(2*pi*omega*t+pi/3);
you can check whether the phase shift calculated by cpsd is pi/3.

how to use ifft function in MATLAB with experimental data

I am trying to use the ifft function in MATLAB on some experimental data, but I don't get the expected results.
I have frequency data of a logarithmic sine sweep excitation, therefore I know the amplitude [g's], the frequency [Hz] and the phase (which is 0 since the point is a piloting point).
I tried to feed it directly to the ifft function, but I get a complex number as a result (and I expected a real result since it is a time signal). I thought the problem could be that the signal is not symmetric, therefore I computed the symmetric part in this way (in a 'for' loop)
x(i) = conj(x(mod(N-i+1,N)+1))
and I added it at the end of the amplitude vector.
new_amp = [amplitude x];
In this way the new amplitude vector is symmetric, but now I also doubled the dimension of that vector and this means I have to double the dimension of the frequency vector also.
Anyway, I fed the new amplitude vector to the ifft but still I don't get the logarithmic sine sweep, although this time the output is real as expected.
To compute the time [s] for the plot I used the following formula:
t = 60*3.33*log10(f/f(1))/(sweep rate)
What am I doing wrong?
Thank you in advance
If you want to create identical time domain signal from specified frequency values you should take into account lots of details. It seems to me very complicated problem and I think it need very strength background on the mathematics behind it.
But I think you may work on some details to get more acceptable result:
1- Time vector should be equally spaced based on sampling from frequency steps and maximum.
t = 0:1/fs:N/fs;
where: *N* is the length of signal in frequency domain, and *fs* is twice the
highest frequency in frequency domain.
2- You should have some sort of logarithmic phases on the frequency bins I think.
3- Your signal in frequency domain must be even to have real signal in time domain.
I hope this could help, even for someone to improve it.

MATLAB Code Clarification

I recently visited this page in order to determine the frequency from signal data in MATLAB:
Determine frequency from signal data in MATLAB
And in this page, an answerer responded with the following code:
[maxValue,indexMax] = max(abs(fft(signal-mean(signal))));
From what I can see, a Fast Fourier Transform is taken on a signal named signal, its magnitude is kept by using 'abs', and the max value is computed. The max value will be in maxValue, and the indexMax will contain the position of the maxValue. However, can someone explain what is meant by signal-mean, and what the purpose of it?
It basically normalize the vector signal so it has mean zero (subtracts the mean from signal). So signal - mean(signal) looks like signal except that is shifted on the y axis so it has a zero mean. Hope it is clear.
In the example you posted in the link, the mean of the signal is around -2, so by subtracting the mean you end up with a the signal shifted up around the y=0 axis.
As stated in vsoftco's answer, signal-mean(signal) subtracts the mean of the signal.
However, the key point is: why is this is done? If you don't subtract the mean, it's very likely that the maximum peak in the FFT appears at frequency 0 (DC component). But you don't want to detect that as the "frequency" of your signal, even if it truly is the highest spectral component. So you remove that zero-frequency component by subtracting the mean. That way, the max operation will detect the maximum non-zero frequency component, which is probably what you want.