How to do autocorrelation with a given data

22 Ansichten (letzte 30 Tage)
Sasha
Sasha am 27 Okt. 2024
Kommentiert: Image Analyst am 28 Okt. 2024
Hi, Could you help me please? I have data (t,A). t stands for time, A for Absorbance, and i have it in txt file. I'd like to analyze it with autocorrelation function. I tried writing the code, I did just like how i understand what i read the mathworks site 'Autocorr'. But simply saying, i'm afraid the data 't' that i have is not included in the process with my coding. Could I get more explanation, or a validation maybe? thank you.
just for example the data is
t = 3:3:30
A = 3.523;3.523;3.43;3.323;3.43;3.43;3.323;2.923;2.723;2.923;
[t,A]=textread('Data.txt')
[acf,lags]=autocorr(A)

Akzeptierte Antwort

Pavl M.
Pavl M. am 27 Okt. 2024
Bearbeitet: Pavl M. am 27 Okt. 2024
clc
clear all
close all
stdv = 4;
norm_option = 'normalized'; %unbiasd, biased
t = 3:3:30;
A = [3.523;3.523;3.43;3.323;3.43;3.43;3.323;2.923;2.723;2.923];
%OR:
% [t,A]=textread('Data.txt')
subplot(2, 1, 1);
plot(t, A, 'b-', 'LineWidth', 2);
grid on;
xlabel('t');
ylabel('A')
title('Original Data')
%Next will incorporate in full 't' in the process:
[autoCorrA,l] = autocorr(A,NumLags=length(t)-1,NumSTD=stdv);
autocorrB = xcorr(A,norm_option)
autocorrB = 19×1
0.0965 0.1864 0.2804 0.3848 0.4957 0.6058 0.7092 0.8057 0.9005 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
autoCorrA
autoCorrA = 10×1
1.0000 0.6892 0.1810 -0.0982 -0.0910 -0.1166 -0.2491 -0.3870 -0.3094 -0.1189
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
l
l = 10×1
0 1 2 3 4 5 6 7 8 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
subplot(2, 1, 2);
%original time included
plot(t,autoCorrA, 'b-', 'LineWidth', 2);
grid on;
xlabel('Index');
ylabel('ACF')
title('ACF')
figure
plot(t,autocorrB(1:2:length(autocorrB)), 'b-', 'LineWidth', 2);
grid on;
xlabel('Index');
ylabel('XCorr')
title('Raw normalized auto cross-correlation')
%Constructed from needing help code by
%https://independent.academia.edu/PMazniker
%+380990535261
%https://diag.net/u/u6r3ondjie0w0l8138bafm095b
%https://github.com/goodengineer
%https://orcid.org/0000-0001-8184-8166
%https://willwork781147312.wordpress.com/portfolio/cp/
%https://www.youtube.com/channel/UCC__7jMOAHak0MVkUFtmO-w
%https://nanohub.org/members/130066
%https://pangian.com/user/hiretoserve/
%https://substack.com/profile/191772642-paul-m
  1 Kommentar
Sasha
Sasha am 28 Okt. 2024
Thanks for your explanation, it's really helpful, but actually I'm a little bit confused why did you pick '4' as stdv. I read it the default number for NumSTD is 2. Thank you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 27 Okt. 2024
I don't have the Econometric Toolbox so I can't use autocorr but I can use the regular xcorr. Maybe this will help you:
t = 3:3:30;
A = [3.523;3.523;3.43;3.323;3.43;3.43;3.323;2.923;2.723;2.923];
subplot(2, 1, 1);
plot(t, A, 'b-', 'LineWidth', 2);
grid on;
xlabel('t');
ylabel('A')
title('Original Data')
% [t,A]=textread('Data.txt')
% [acf,lags]=autocorr(A)
autoCorrA = xcorr(A);
subplot(2, 1, 2);
plot(autoCorrA, 'b-', 'LineWidth', 2);
grid on;
xlabel('Index');
ylabel('Autocorrelation Value')
title('Autocorrelation Signal')
  2 Kommentare
Sasha
Sasha am 28 Okt. 2024
Thank you for you explanation, since i still can't understand the difference between autocorr and xcorr usage, so actually I needed it with autocorrelation function. Do you mind give me a simple hint? like when I should use autocorr and when it's better with xcorr..
Image Analyst
Image Analyst am 28 Okt. 2024
I'm not really familiar with the autocorrelation function. It seems different than the usual one we all know. They say
"
The autocorrelation function measures the correlation between the univariate time series yt and yt + k, where k = 0,...,K and yt is a stochastic process.
According to [1], the autocorrelation for lag k is
rk=ckc0,
where
  • ck=1TTkt=1(yty)(yt+ky).
  • c0 is the sample variance of the time series.
Suppose that q is the lag beyond which the theoretical ACF is effectively 0. Then, the estimated standard error of the autocorrelation at lag k > q is
SE(rk)=1T(1+2qj=1r2j).
If the series is completely random, then the standard error reduces to 1/T.
"
You probably know more about that function than me. Sorry but I don't really know when you'd use that. I'm guessing it's something special for econometrics applications.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Time Series Events finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by