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 回表示 (過去 30 日間)
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.

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 4 月 14 日
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 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 4 月 15 日
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))
SA
SA 2021 年 4 月 15 日
編集済み: SA 2021 年 4 月 16 日
Thanks for your question. File size is big as I consider longer time (10,000+ seconds). I want to check the Real Coherence value at a specific frequency(say @40Hz =(coh(42))). Here, the sampling frequency is chosen fs=500 and 'noverlap' left blank which matlab set 50% (default).
so, I tried the following scripts.
clc; close all;
p = dir('f*.txt');
nFiles = numel(p);
counter=0;
val = zeros(nFiles); % Memory Pre-allocation
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);
fclose(fileID1);
for i = (kk+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,~]=cpsd(f2,f2,hanning(512),[],512,500);
[Pxy,~]=cpsd(f1,f2,hanning(512),[],512,500);
[Pxx,~]=cpsd(f1,f1,hanning(512),[],512,500);
coh= (Pxy)./sqrt(Pxx.*Pyy);
realCoh=real(coh(42));
val(kk,i)=val(i,kk)+realCoh;
end
end
final_val=val+val'+eye(nFiles);
If I calculate the half of the symmetric matrix and adding transpose of it, it saves 60% of time. Thanks for your time.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBiological and Health Sciences についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by