Finding the time or rate weighted average between peaks in Matlab? - matlab

I have some data, that basically looks like a sine wave. I run that through the peak detection function to find the peaks and mins of the data:
[Maxima,MaxIdx] = findpeaks(Peak,'MinPeakHeight',mean(Peak),'MinPeakDistance',10);
Mins=1.01*max(Peak)-Peak;
[Minima,MinIdx] = findpeaks(Mins,'MinPeakHeight',mean(Mins),'MinPeakDistance',10);
Minima = Peak(MinIdx);
What I would like to do is calculate the slope between each peak and trough, and then use that slope to calculate a time weighted average minimum value and see how that method compares to the minimum value. How would I go about this?
was asked to show some data:-

Related

how to calculate and plot the fast fourier transform of sla data (3 d matrix) in matlab?

I want to check for the existent dominant peaks in sla data(3 d matrix lon, lat ,time) from which I can delineate what period to choose so that I can filter this data and look for waves propagating in the region. My data is for all lat and lon(global data). However, I want to average over a lon range of 70E to 100E and after that average over 10S to 10N. This average will give a a vector only in time dimension over which I can perform fast fourier transform. Then I want to plot this with time as the x axis. I'm new to matlab, any help is appreciated. Also if anyone has done this before it would be nice if you told me if my logic is correct or not.

Frequency from fft data set matlab [duplicate]

This question already has answers here:
How do I obtain the frequencies of each value in an FFT?
(5 answers)
Closed 6 years ago.
I have a data set in a matrix in matlab. It contains 25,000 values taken every 0.5 ns; so the total time of the dataset is 1.25E-5 seconds.
The data set contains very high frequency noise that I am not interested in so I create another matrix is every 50th data point from the first matrix So the size of the matrix is 1000*.
I plot the absolute values from matlab's fft this matrix (I also normalise the amplitude and only plot the first half) and get the attached (two plots, second is a close up of the low frequencies I am interested in). How do I convert the x-axis to frequency?
Another point, if I take every data point (so I create an fft of the entire 25,000 points) then the x-axis is exactly the same; in other words, the size of my matrix seems to have no bearing on the x-axis returned by matlab. I've attached two links to the frequency spectrum, one of which is a close-up of the low frequencies I am interested in. It's axis goes from 0-50, so it is these values I need to convert to Hz.
Thankyou in advance!
Close up of frequency spectrum
frequency spectrum
From what I read on http://www.mathworks.com/help/matlab/math/fast-fourier-transform-fft.html#bresqop-1, it appears that the units on the x-axis of the plotted FFT are Hz if the first vector, f, you put into the plot(f,power), is defined as a sequence of n elements (n being the number of data points put into the FFT) increasing from zero to the sample frequency.
Thus, for the first plot, which used every 50th of points that were taken at a frequency of 2 GHz, the sample frequency would be 40 MHz. Thus, f = (0:n-1)*4*10^7/(25000/50)
It goes on to show how to use the fftshift function to put the center of the output of the fft function at 0, but it's clear you already did that and chopped off the negative part.
So, once you have the right separation of fs/n, sampling frequency divided by number of data points used, in the vector that supplies the x-axis to the plot function, then the units of the x-axis will be Hz.
(I hope you still have the numbers to graph again? If not, this question might help: Confusion in figuring out the relation between actual frequency values and FFT plot indexes in MATLAB)

MATLAB sine wave plot is not correct

I am new to MATLAB and I wrote some code to generate a sine wave. However the graph is not correct. Here is the screenshot of my code and the plot
What is the problem? Please help!
MATLAB plots discrete points and simply draws a straight line to connect neighbouring points together. Your time points are one second (1s) in between, and you are specifying a frequency of 100 Hz. In addition, because your sampling time is a multiple of the period of your sine wave, substituting all of those values of t would thus make the sin result equal to 0, though there is some numerical imprecision. Specifically, if you look at the y-axis, you'll see that the magnitude of your numbers is around 10^{-13}. However even if you escape this, the sampling time is TOO LARGE for the specified frequency of your wave and so this huge gap in between points is visualized as that jagged wave that you see in your graph.
The solution is to simply make your sampling time smaller. Try something small, like 1e-4 for example:
t = 0:1e-4:0.05;
f = 100;
A = 2;
x = A*sin(2*pi*f*t);
plot(t,x);
We get this now:

How to measure the period between the peaks or lows of waves?

I want to measure the period between the peaks/lows of the adjacent waves shown in the figure.
This is an oscillatory behavior of calcium concentration in a cell. The peaks are not the same, hence I would need to calculate the peak/lows for each wave, obtain the corresponding time associated with the peaks/lows, and find the difference between adjacent peaks/lows. I have stored the calcium concentration values for every time "0.01".
Can anyone suggest me how I should code it? I would prefer to use smaller lines of code.
Look into the inbuilt findpeaks function that can return you the index of peaks in your signal.
You could use this to also find the lows in your signal by first squaring your signal. Something like this could work (I haven't tried this in MATLAB so there could possibly be some syntax issues):
% Square the signal so that the lows become peaks
signal = signal .^ 2;
% Get the location of the peaks in terms of t (your time vector)
[~, peaksAndLows] = findpeaks(signal,t)
% Find the difference between consecutive peaks/lows
periodsBetweenPeaksAndLows = diff(peaksAndLows);

Specific part of an impulse response matlab

I measure the impulse response of a microphone. At beginning of plot i have some delay and then highest value comes at 40 ms and decreases till 45 ms.
When I take fourier transform, I only want to use the part between 35-45 ms. when I use different microphones, delay decreases or increases so the peak value is shifting. So, the time range I want also changes. How I can get that range, which contains highest value, automaticaly in MATLAB?
Assuming you have a vector y of measurements and want an interval of length 2r+1 measurements you could do:
center = find(y==max(y)); % find the peak value
y_edited = y(center-r:center+r); % look at r samples before and after this peak
Then perform the fourier transform on y_edited. Note that having noise on your signal might affect your performance.