Help with Cross-correlation and Auto-correlation

Hi
I don’t have that much experience in MATLAB, I hope someone can point me in the right direction, I am looking at breathing and the rate and various ways to detect this. I am currently looking into Cross correlation and auto correlation can somebody please help me setup some basic code and give me a few pointers.
I have attached a file which has 6000 samples, 100 samples per second, over 60 seconds.
Any help would be greatly appreciated.
Kind Regards
David

 Réponse acceptée

Try this:
s = load('raw_data.mat')
y = s.raw_ad_data_sine;
plot(y, 'b-', 'LineWidth', 2);
grid on;
% Find peaks
[peakValues, indexesOfPeaks] = findpeaks(y, 'MinPeakDistance', 200, 'Threshold', 1000);
% Plot them.
hold on;
plot(indexesOfPeaks, y(indexesOfPeaks), 'rv', 'LineWidth', 2, 'MarkerSize', 10);
% Find valleys
[valleyValues, indexesOfValley] = findpeaks(-y, 'MinPeakDistance', 200, 'Threshold', 1000);
valleyValues = -valleyValues;
% Plot them.
hold on;
plot(indexesOfValley, y(indexesOfValley), 'r^', 'LineWidth', 2, 'MarkerSize', 10);

5 commentaires

HI
Thanks for pointing me in this direction, I have changed the samples now to be centred around zero can you please show me how to setup up your script to be able to detect the peaks (with correct timing 60 seconds) with this different sample sorry if this is basic but I have never used Peak before.
Attached new data file
Thank you for your help
David
David, you will just have to try different amounts of data smoothing and parameters to find something that works. There are lots of parameters in sgolayfilt() and findpeaks() and I don't have the time to do the trial and error to optimze them for you. I'll leave that to you but I think you have a start anyway.
% Demo by Image Analyst.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
s = load('raw_data.mat')
y = s.raw_ad_data_sine;
plot(y, 'c.');
% Smooth the data with a Savitzky-Golay filter.
windowWidth = 291;
y = sgolayfilt(y, 4, windowWidth);
hold on;
plot(y, 'b-', 'LineWidth', 2);
grid on;
caption = sprintf('Data smoothed with Savitzy-Golay filter of width %d', windowWidth);
title(caption, 'FontSize', fontSize);
drawnow;
% Find peaks
fprintf('Finding peaks...\n');
[peakValues, indexesOfPeaks] = findpeaks(y, 'MinPeakDistance', 100);
if isempty(peakValues)
fprintf('No peaks found with those parameters. Try adjusting the parameters.\n');
else
% Plot them.
fprintf('Plotting peaks...\n');
hold on;
plot(indexesOfPeaks, y(indexesOfPeaks), 'rv', 'LineWidth', 2, 'MarkerSize', 10);
drawnow;
end
% Find valleys
fprintf('Finding valleys...\n');
[valleyValues, indexesOfValley] = findpeaks(-y, 'MinPeakDistance', 100);
valleyValues = -valleyValues;
if isempty(peakValues)
fprintf('No valleys found with those parameters. Try adjusting the parameters.\n');
else
% Plot them.
fprintf('Plotting valleys...\n');
hold on;
plot(indexesOfValley, y(indexesOfValley), 'r^', 'LineWidth', 2, 'MarkerSize', 10);
end
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m\n', mfilename);
Hi
Thanks for all your help I just have one last question how can I change the plot to show the correct timings ie 60 seconds
David
Not sure what that means but you can create an x axis if you want with linspace. If the total length of y is 60 seconds, you can make x like
x = linspace(0, 60, length(y));
Hi
I am strugling to get the display to show the 60s of data correctly, please see atached display I need to display the last graph (5) with the x axis showing the correct timimng , could you please help.
David

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Measurements and Feature Extraction dans Centre d'aide et File Exchange

Produits

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by