I implemented the following code, where I first extracted the signal relative to the left hand imagery movement and rest. Then I averaged these signals and finally I calculate the power spectrum.

The calc_rsqu function is the same contained in the Offline Analysis's folder and that function return for every frequency and channel the value of 1.

I don't know why? Any suggestion?

Thanks.

Luca

Code: Select all

```
Left=find(Scode==1);
L=length(Left);
signalP=zeros(L,nchannel);
for k=1:nchannel
for i=1:L
j=Left(i);
signalP(i,k)=signal(j,k);
end
end
Rest=find(Scode==0);
Lr=length(Rest);
signalPr=zeros(Lr,nchannel);
for k=1:nchannel
for i=1:Lr
j=Rest(i);
signalPr(i,k)=signal(j,k);
end
end
%Averaging left signal%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=511;
n_epoch=fix(L/(N));
FR=n_epoch;
xav=zeros(N,nchannel);
for k=1:nchannel
for jj=1:FR
xav(:,k)=xav(:,k)+signalP(N*(jj-1)+1:(N*jj),k);
xav(:,k)=xav(:,k)./jj;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Averaging rest signal%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N=767;
n_epoch=fix(Lr/(N));
FR=n_epoch;
xavr=zeros(N,nchannel);
for k=1:nchannel
for jj=1:FR
xavr(:,k)=xavr(:,k)+signalPr(N*(jj-1)+1:(N*jj),k);
xavr(:,k)=xavr(:,k)./jj;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Power spectrum for averaged epoch relative to left hand imagery movement and rest%%%%%%%%%%%%%%%
for k=1:nchannel
[Pxx(:,k),f]=psd(xav(:,k)-mean(xav(:,k)),NFFT,fs,boxcar(NFFT),0);
end
for k=1:nchannel
[Pxxr(:,k),fr]=psd(xavr(:,k)-mean(xavr(:,k)),NFFT,fs,boxcar(NFFT),0);
end
%%%%%%%%r squared value%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:nchannel
for jj=1:length(Pxx)
r2 = calc_rsqu(double(Pxx(jj,k)), double(Pxxr(jj,k)), 1);
P(jj,k)=r2;
end
end
```