N = EEG.pnts; SampleRate = EEG.srate; NFFT = 2^nextpow2(N); Freq = SampleRate/2*linspace(0,1,NFFT/2+1); for chan = 1:size(EEG.data,1) for epochs = 1:size(EEG.data,3) ffts(:,chan,epochs) = fft(squeeze(EEG.data(chan,:,epochs)),NFFT)/N; end end for channel1 = 1:size(EEG.data,1) for channel2 = 1:size(EEG.data,1) fx = squeeze(ffts(:,channel1,:)); Pxx = fx.*conj(fx)/N; MeanPx = mean(Pxx,2); fy = squeeze(ffts(:,channel2,:)); Pyy = fy.*conj(fy)/N; MeanPy = mean(Pyy,2); Pxy = fx.*conj(fy)/N; MeanPxy = mean(Pxy,2); C = (abs(MeanPxy).^2)./(MeanPx.*MeanPy); before_coh(:,channel1,channel2,f) = C; end end帮我把这段代码改成特定频率4-8
时间: 2023-12-11 07:03:21 浏览: 163
json_jar.rar_2QV_EEG_json.jar_json解析的jar_morph.jar
好的,你可以尝试以下修改:
```
N = EEG.pnts;
SampleRate = EEG.srate;
NFFT = 2^nextpow2(N);
Freq = SampleRate/2*linspace(0,1,NFFT/2+1);
% 选取4-8Hz的频率范围
freq_range = Freq >= 4 & Freq <= 8;
Freq = Freq(freq_range);
for chan = 1:size(EEG.data,1)
for epochs = 1:size(EEG.data,3)
ffts(:,chan,epochs) = fft(squeeze(EEG.data(chan,:,epochs)),NFFT)/N;
end
end
for channel1 = 1:size(EEG.data,1)
for channel2 = 1:size(EEG.data,1)
fx = squeeze(ffts(freq_range,channel1,:));
Pxx = fx.*conj(fx)/N;
MeanPx = mean(Pxx,2);
fy = squeeze(ffts(freq_range,channel2,:));
Pyy = fy.*conj(fy)/N;
MeanPy = mean(Pyy,2);
Pxy = fx.*conj(fy)/N;
MeanPxy = mean(Pxy,2);
C = (abs(MeanPxy).^2)./(MeanPx.*MeanPy);
% 存储4-8Hz的coherence结果
before_coh(:,channel1,channel2) = C;
end
end
```
这里的修改主要是在选择频率范围这一部分,根据要求选取了4-8Hz的频率范围。然后在循环中只保留了特定频率范围的数据,并且将最后的结果存储在`before_coh`变量中。
阅读全文