Running a function multiple times and recording outputs for each of the runs in one table.
1 回表示 (過去 30 日間)
古いコメントを表示
I have a set of data (125,000 rows, 38 columns) and I want to run this code for each column in a matrix starting from column 2. Then, create 2 tables from the outputs. Colum ONE is the time ( a common column) that is used in the calculations.
Here are the codes:
%import xlsx data
dat=importdata('DataBook.xlsx');
t=dat.data(:,1); %time
so=dat.data(:,2); %original signal( this is only for one column, I need the same code to repeat itself for the remaining columns (from 2 to 38)
%frequency space
kHz=1e3;
f_us=27.441*kHz;
T_us=1/f_us;
dt=t(2)-t(1);
Fs=1/dt; %sampling frequency
f=linspace(1,Fs,length(t));
%Fourier Transform
SO=fft(so);
figure(1); plot(f,abs(SO)); xlim([0 Fs/2]); xlabel('Frequency [Hz]');
%theoretical undersampling frequency
ncycle=ceil(dt/T_us); %=9
tau=T_us*ncycle-dt;
T_fake_theory=(T_us/tau)*dt;
F_fake_theory=1/T_fake_theory; %684 Hz
%practical undersampling frequency due to stage move
SOa=abs(SO);
F_fake=f(find(SOa(1:round(length(SOa)/2))==max(SOa))); %663.9 Hz
T_fake=1/F_fake;
%IQ-signal
s_cos=so.*cos(2*pi*F_fake*t);
s_sin=so.*sin(2*pi*F_fake*t);
%move-mean
N_mean_cycle=100;
mean_point=round((N_mean_cycle*T_fake)/dt);
mean_cos = 2*movmean(s_cos,[mean_point mean_point]);
mean_sin = 2*movmean(s_sin,[mean_point mean_point]);
mean_abs=sqrt(mean_cos.^2+mean_sin.^2); %result of A(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)
mean_phase=atan2(mean_sin,mean_cos); %result of theta(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)
0 件のコメント
採用された回答
VBBV
2023 年 5 月 14 日
Assuming some random data, the output data can be fit into a table as shown below
% some random data
data= randi([2 200],125000,38); %importdata('DataBook.xlsx');
t=data(:,1); %time
so=data(:,2:38);
%original signal( this is only for one column,
% I need the same code to repeat itself for the remaining columns (from 2 to 38)
%frequency space
kHz=1e3;
f_us=27.441*kHz;
T_us=1/f_us;
dt=t(2)-t(1);
Fs=1/dt; %sampling frequency
f=linspace(1,Fs,length(t));
%Fourier Transform
SO=fft(so);
% figure(1); plot(f,abs(SO)); xlabel('Frequency [Hz]');
%theoretical undersampling frequency
ncycle=ceil(dt/T_us); %=9
tau=T_us*ncycle-dt;
T_fake_theory=(T_us/tau)*dt;
F_fake_theory=1/T_fake_theory; %684 Hz
%practical undersampling frequency due to stage move
SOa=abs(SO);
for k = 1:size(SOa,2)
F_fake(:,k)=f(find(SOa(1:round(length(SOa)/2),k)==max((SOa(:,k)))));
end %663.9 Hz
T_fake=1./F_fake;
%IQ-signal
s_cos=so.*cos(2*pi*F_fake.*t);
s_sin=so.*sin(2*pi*F_fake.*t);
%move-mean
N_mean_cycle=100;
% mean_point=round((N_mean_cycle*T_fake)./dt)
% mean_cos = 2*movmean(s_cos,[mean_point mean_point+5]);
% mean_sin = 2*movmean(s_sin,[mean_point mean_point+5]);
% mean_abs=sqrt(mean_cos.^2+mean_sin.^2)
%result of A(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)
% mean_phase=atan2(mean_sin,mean_cos)
TT = table(s_cos,s_sin,'VariableNames',{'s_cos','s_sin'})
5 件のコメント
VBBV
2023 年 5 月 15 日
編集済み: VBBV
2023 年 5 月 15 日
the mean_point must be a positive scalar value if you want to use movmean function, The old syntax for movmean that was used in your code needs to be used ONLY for a vector NOT for a matrix.
% some random data
data= randi([2 200],125000,38); %importdata('DataBook.xlsx');
t=data(:,1); %time
so=data(:,2:38);
%original signal( this is only for one column,
% I need the same code to repeat itself for the remaining columns (from 2 to 38)
%frequency space
kHz=1e3;
f_us=27.441*kHz;
T_us=1/f_us;
dt=t(2)-t(1);
Fs=1/dt; %sampling frequency
f=linspace(1,Fs,length(t));
%Fourier Transform
SO=fft(so);
% figure(1); plot(f,abs(SO)); xlabel('Frequency [Hz]');
%theoretical undersampling frequency
ncycle=ceil(dt/T_us); %=9
tau=T_us*ncycle-dt;
T_fake_theory=(T_us/tau)*dt;
F_fake_theory=1/T_fake_theory; %684 Hz
%practical undersampling frequency due to stage move
SOa=abs(SO);
for k = 1:size(SOa,2)
F_fake(:,k)=f(find(SOa(1:round(length(SOa)/2),k)==max((SOa(:,k)))));
end %663.9 Hz
T_fake=1./F_fake;
%IQ-signal
s_cos=so.*cos(2*pi*F_fake.*t)
s_sin=so.*sin(2*pi*F_fake.*t)
% simple mean
mean_abs=sqrt(mean(s_cos.^2)+mean(s_sin.^2))
%result of A(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)
mean_phase=atan2(mean(s_sin),mean(s_cos))
N_mean_cycle=100;
% the mean point must be a positive value if you want to use movmean
mean_point=round((N_mean_cycle*T_fake)./dt)
% this syntax needs to be used ONLY for a vector NOT for matrix
% mean_cos = 2*movmean(s_cos,[mean_point mean_point+5]);
% mean_sin = 2*movmean(s_sin,[mean_point mean_point+5]);
% this syntax is used for matrix
mean_cos = 2*movmean(s_cos,max(mean_point), 2);
mean_sin = 2*movmean(s_sin,max(mean_point), 2);
mean_abs=sqrt((mean_cos.^2)+(mean_sin.^2))
%result of A(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)
mean_phase=atan2(mean_sin,mean_cos)
T1 = table(t,'VariableNames',{'time'}) % time
T2 = table(mean_cos,mean_sin,'VariableNames',{'mean_cos','mean_sin'}) % outputs
T_table = cat(2,T1,T2)
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!