MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# How we can calculate Power spectrum of EEG signal?

Asked by siddhartha chandel on 6 Sep 2012

Dear all . I want to calculate power spectrum of single channel eeg . I have coded following but it gives error ??? Subscript indices must either be real positive integers or logicals.

Error in ==> modemotion at 17 plot(ff,pp(0:63)); ylabel('power spectrum density');xlabel('frequency');title('signal power spectrum'); Please give me how i can short out this. %%%%% Program to calculate the power spectrum of EEG signal%%%% clear all; clc; data=xlsread('C:\Users\SID\Desktop\emotion regulation post2\emotion\shim1.xlsx'); figure(1); xlabel('sample'); ylabel('magnitude'); plot(data); legend('14 channel plot of subject1 BASELINE'); data= data'; value1=data(1,:); chan1= value1-mean(value1); fs=128;d=1/fs; t=[0:length(chan1)-1]*d; figure(2); plot(chan1);title('original signal'); fs=fft(chan1,128); pp=fs.*conj(fs)/128; ff=(0:63)/128/d; figure(3); plot(ff,pp(0:63)); ylabel('power spectrum density');xlabel('frequency');title('signal power spectrum');

## 1 Comment

Star Strider on 6 Sep 2012
``` %%%%% Program to calculate the power spectrum of EEG signal%%%%
clear all;
clc; ```
```data=xlsread('C:\Users\SID\Desktop\emotion regulation post2\emotion\shim1.xlsx');
```
```figure(1);
xlabel('sample');
ylabel('magnitude');
plot(data);
legend('14 channel plot of subject1 BASELINE');
```
```data= data';
value1=data(1,:);
chan1= value1-mean(value1);
fs=128;
d=1/fs;
t=[0:length(chan1)-1]*d;
```
```figure(2);
plot(chan1);
title('original signal');
```
```fs=fft(chan1,128);
pp=fs.*conj(fs)/128;
ff=(0:63)/128/d;
```
```figure(3);
plot(ff,pp(0:63));
ylabel('power spectrum density');
xlabel('frequency');
title('signal power spectrum');
```

## Products

No products are associated with this question.

## 1 Answer

Answer by Star Strider on 6 Sep 2012
Edited by Star Strider on 6 Sep 2012

The problem with this line:

```plot(ff,pp(0:63));
```

is that in MATLAB, zero is not a valid index element. Subscript indices have to be integers >= 1, or logicals.

Change it to:

```plot(ff,pp(1:64));
```

and your code should work.

You can also replace:

```pp=fs.*conj(fs)/128;
```

with:

```pp=abs(fs)/128;
```

Contact us