I want to do loop operation of these files but fail to get the desired result. Can anyone help me to figure out the problem?
1 view (last 30 days)
Show older comments
After the operation, I wish to get the values as in the table:
The script that I work on, can't store/save the data as in the table format.
p = dir('f*.txt');
nFiles = numel(p);
counter=0;
for kk = 1:nFiles
counter= kk+1;
fileID1 = fopen(p(kk).name,'rt');
C_data = textscan(fileID1,'%f');
f1 = cell2mat(C_data); % A = dlmread(p(k).name);
%A_cat{kk} = A; disp(size(A_cat));
fclose(fileID1);
val = cell(1, nFiles-1); % Memory Pre-allocation
for i = counter: nFiles
fileID2 = fopen(p(i).name,'rt');
C_data = textscan(fileID2,'%f');
f2 = cell2mat(C_data); % A = dlmread(p(k).name);
fclose(fileID2);
[Pyy,freq]=cpsd(f2,f2,hanning(512),[],512,500);
[Pxy,freq]=cpsd(f1,f2,hanning(512),[],512,500);
[Pxx,freq]=cpsd(f1,f1,hanning(512),[],512,500);
coh= (Pxy)./sqrt(Pxx.*Pyy);
val=real(coh(42))
%val=horzcat(val)
end
val(kk)=horzcat(val)
end
Can anyone help to fix the issues? Thank you in advance.
0 Comments
Accepted Answer
Mathieu NOE
on 14 Apr 2021
hello
I tweaked a bit your code , if fact it was simpler in my opinion to do the full 4 x 4 loops instead of doing the specific counter condition
now there is one point that surprises me, its the diagonal of your matrice . the autocorrelation of each element with itself should be 1 and not zero (and it's what I get) - so I wonder about this point. of course we can change the code to force the diagonal to be zero but that sounds strange to me
p = dir('f*.txt');
nFiles = numel(p);
counter=0;
for kk = 1:nFiles
%counter= kk+1
fileID1 = fopen(p(kk).name,'rt');
C_data = textscan(fileID1,'%f');
f1 = cell2mat(C_data); % A = dlmread(p(k).name);
%A_cat{kk} = A; disp(size(A_cat));
fclose(fileID1);
%val = cell(1, nFiles-1); % Memory Pre-allocation
for i = 1: nFiles
fileID2 = fopen(p(i).name,'rt');
C_data = textscan(fileID2,'%f');
f2 = cell2mat(C_data); % A = dlmread(p(k).name);
fclose(fileID2);
[Pyy,freq]=cpsd(f2,f2,hanning(512),[],512,500);
[Pxy,freq]=cpsd(f1,f2,hanning(512),[],512,500);
[Pxx,freq]=cpsd(f1,f1,hanning(512),[],512,500);
coh= (Pxy)./sqrt(Pxx.*Pyy);
val(kk,i)=real(coh(42)) ;
end
end
% gives :
val =
1.0000 0.6725 -0.2602 0.3779
0.6725 1.0000 0.1127 0.2854
-0.2602 0.1127 1.0000 0.0763
0.3779 0.2854 0.0763 1.0000
5 Comments
Mathieu NOE
on 15 Apr 2021
hello
so you are doing a cross correlation matrix based on time series , that's what I understand today
why are the data files so big ? are you sure that you need all this amount of data to process, for example maybe your files at sampled at too high sampling freq and this could be reduced to speed up the process (using decimate); have you conducted a king of frequency analysis to define in which frequency range the data must be ?
second, why do you pick only the 42th value of the coherence ? (coh(42))
More Answers (0)
See Also
Categories
Find more on Biological and Health Sciences in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!