Offline Analysis

Forum for discussion on different signal processing algorithms
Locked
lucamossa
Posts: 19
Joined: 27 Jan 2014, 08:46

Offline Analysis

Post by lucamossa » 06 Jun 2014, 08:03

Hi community, I'm trying to replicate what does the Offline analysis, in particular the frequency analysis of the Mu rhythm.
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

boulay
Posts: 382
Joined: 25 Dec 2011, 21:14

Re: Offline Analysis

Post by boulay » 06 Jun 2014, 09:41

There are many things fundamentally wrong with your code. I wrote a few notes below, but I think you need guidance from someone from your institution who has experience in signal processing.

There should be no need to average anything in this type of analysis. However, if you want to average then you must do the PSD BEFORE averaging.

In the future, if you do have to average something then simply use Matlab's 'mean' function. In your averaging code you were dividing by jj on every loop. You should only need to divide the total by FR after you've summed all the epochs.

The way you construct your data is wrong. The problem is that some trials will not be exactly 511 samples. The way you are concatenating all your samples together then cutting them up into 511-sample segments will lead to you cutting between trials at the wrong place. Some trials will have pieces of other trials with them. That will cause severe discontinuities in your data and will create huge problems for your frequency transform.

The following code is incomplete and is meant only as a hint.

Code: Select all

lstart_indices = 1 + find(Scode(2:end)==1 & Scode(1:end-1)==0);
if Scode(1) == 1
    lstart_indices = [1 lstart_indices]; %Assume that the first sample was a 'start' if its Scode value was 1
end

%Do the same for rest

%For each trial, extract N (=511?) samples from its start index. Keep N the same for imagery and rest conditions.

%Do the PSD

%For each channel, for each frequency returned by the PSD, calculate the rsq between imagery and rest USING ALL TRIALS.

lucamossa
Posts: 19
Joined: 27 Jan 2014, 08:46

Re: Offline Analysis

Post by lucamossa » 11 Jun 2014, 05:15

Hi Chad, thanks for your hints. I modified the first part of the code.
Then, I don't understand the last two point:
Once I constructed the data I should do the PSD of each trial or do the PSD of the entire signals (i.e. the signal containing in each trial the left hand imagery movement and the signal containing the rest condition)?
I tried different combination but the r squared value is 1 for each combination of channels and frequencies.

Thank you very much.

Luca

lucamossa
Posts: 19
Joined: 27 Jan 2014, 08:46

Re: Offline Analysis

Post by lucamossa » 11 Jun 2014, 08:34

I solved the problem. I inserted in the calc_rsqu function the right format of the data (dim1 by dim2 by trial).

Thanks again Chad.

Locked

Who is online

Users browsing this forum: Bing [Bot] and 1 guest