關於靜息態求頻域的腳本
%
% Written By Yizhou
% resting or praying
% Using the code without proper understanding the code and relevant background
% of EEG may lead to confusion, incorrect data analyses,or misinterpretations
% of results.
% The author assumes NO responsibility for inappropriate or incorrect use
% of this code.
% WX: 17373158786
%
%頻域分析設置
% Fs採樣率
Fs = 512;
%分段長度 256*2
L = 1024;
%T = 1/Fs;
%t = (0:L-1) * T;
NFFT = 2^nextpow2(L);
%創建頻域列表
f = Fs/2 * linspace(0,1,NFFT/2+1);
% 路徑設置爲預處理的文件夾
anal_path = 'E:\Docu\Work\1_Projects\Rest_8\anal_V2\resting';cd(anal_path);
files = dir([anal_path,filesep,'R_ICA*resting.set']);
if ~exist([anal_path,filesep,'fft_result'],'dir')
mkdir([anal_path,filesep,'fft_result']);
end
fre_path = [anal_path,filesep,'fft_result'];%%% 頻域結果儲存路徑
cd(fre_path);
for sub_No = 1:length(files)
setname = files(sub_No).name;
EEG = pop_loadset([anal_path, filesep, setname]);
%對於所有通道
for ii=1:size(EEG.data,1)
%對於所有分段
for jj=1:size(EEG.data,3)
%提取第ii個通道 第jj個分段 所有時間點數據
y = squeeze(EEG.data(ii,:,jj));
%做FFT變換 求真實幅值----------------------------------指標1:幅值
temp = fft(y,NFFT)/ L;
fuzhi(jj,:) = 2*abs(temp(1:NFFT/2+1));
% 做FFT變換,求功率(uV平方)%-------------------------指標2:功率
temp = fft(y,NFFT)/L;
power(jj,:)=(2*abs(temp(1:NFFT/2+1))).^2; % fft results, in power
% 做FFT變換,求功率譜密度(PSD)(單位 uV平方/ Hz)%----指標3:功率譜密度
temp = 2*abs(fft(y,NFFT)).^2/L/Fs;
PSD(jj,:) = temp(1:NFFT/2 + 1);
% 做FFT變換,求功率譜密度(PSD)(單位 dB )%----------指標4:功率譜密度
temp = 2*abs(fft(y,NFFT)).^2/L/Fs;
DB_PSD(jj,:) = 10*log10(temp(1:NFFT/2+1));
end
%數據彙總:被試*通道*頻率
mean_fuzhi(sub_No ,ii,:) = squeeze(mean(fuzhi,1)); clear fuzhi;
mean_power(sub_No ,ii,:) = squeeze(mean(power,1)); clear power;
mean_PSD(sub_No ,ii,:) = squeeze(mean(PSD,1)); clear PSD;
mean_DB_PSD(sub_No ,ii,:) = squeeze(mean(DB_PSD,1)); clear DB_PSD;
end
% 保存各個指標
end
% 保存結果文件到當前路徑,代碼中爲fft_results;
save ('fuzhi.mat', 'mean_fuzhi');
save ('power.mat', 'mean_power');
save ('PSD.mat', 'mean_PSD');
save ('DB_PSD.mat', 'mean_DB_PSD');
% % clear mean*;