Can we extract amplitude of levels in a signal using "findchangepts" function?

Hi, I want to find points of abrupt changes in my data signal to obtain various levels in the signal. I am using 'findchangepts' function. I am able to find correct indexes of change points. I also want to retrieve amplitude of each level. For reference, I have attached image of one of my signals operated with 'findchangepts' function. I want to find values of levels shown in "red color".
I am new to MATLAB. Also, can suggest any other method to find changes in signal (my data contains very sharp changes for small duration, appear like sharp peaks in XRD)?
findchangepts(x, 'Statistic', 'mean', 'Threshold', 0.2);

 Accepted Answer

You have to calculate the interval means yourself. That can be done in a loop.
The Code
t = 0:50; % Create Data
x = 50 + sin(2*pi*t/40) + cos(2*pi*t*10/50)*0.1; % Create Data
figure(1)
plot(t, x);
grid
ipt = findchangepts(x, 'Statistic', 'mean', 'MinThreshold', 0.2); % Get Change Point Indices
intvls = [1 ipt length(t)]; % Define Intervals
for k1 = 1:length(intvls)-2
interval_means(k1) = mean(x(intvls(k1):intvls(k1+1)-1)); % Calculate Means For Each Interval
end
interval_means(k1+1) = mean(x(intvls(k1):intvls(end))); % Calculate Means For Last Interval
figure(2)
findchangepts(x, 'Statistic', 'mean', 'MinThreshold', 0.2); % Plot Change Points
In order to avoid overlaps in the change point indices (so the value at the same index is not included in successive intervals), the end-points of each interval are not included in the interval. It is necessary to calculate the last interval separately so it does include the value of the end of the last interval.
This should do what you want.

6 Comments

Thank you for your response. I thought so. Is there any other way besides CUSUM to find point changes and fit the data?
My pleasure.
Other than findpeaks, I am not aware of any. You can create your own versions by experimenting with combinations of the sgolayfilt function (to smooth the data), gradient and del2 (to calculate the derivatives of the smoothed data) and simple thresholding. I have done versions of those when I needed functions to do specific procedures.
If you are looking for zero-crossings, I wrote this little utility function a while back that finds the approximate indices of zero-crossings:
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
It may return a false zero-crossing at the end due to the wrap-around used in the algorithm, so check for that if you use it.
I cannot find anything else in the documentation. There could be some File Exchange contributions that could perform specific functions that may specifically apply to your problem.
Thank you for your answer. I used findchangepts function. It worked.
There appears to be a minor error in the last line of this code snippet. The starting index for x should be k1+1 not k1.
interval_means(k1+1) = mean(x(intvls(k1):intvls(end)));
should read as,
interval_means(k1+1) = mean(x(intvls(k1+1):intvls(end)));

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!