FFT of EEG data
Show older comments
This code is for doing FFT on eeg data to findout the spectral amplitudes of Fundamental frequency and its harmonics,H2,h3 and H4.
Fundamental frequency is 100Hz
time window is from 0milliseconds to 156 milliseconds.
Sampling frequency is 20000Hz
Wheni run the code the following error is shown.
Unable to perform assignment because the left and right sides have a different number of elements.
Error in ff (line 70)
sample(1:length(env)) = env'.*sample(1:length(env));
Following is the code
% The input file should be in a txt format and all the headers should be
% removed. The file should only contain the waveform and no other details
% and should be placed in aseparate folder in the matlab directory. %%
clear all;clc
epoch = 156; %the total epoch duration in milliseconds
prestim = 0; % pre-stimulus time ignoring the negative sign
FFTstart = 0; %starting time range to run FFT
FFTend = 130 ; %ending time range to run FFT
fund = 100;
bin = 10;
%% Do not edit beyond this point
post = epoch - prestim; %post stimulus time
lowf0=fund-bin; %F0 starting frequncy
highf0=fund+bin; %F0 ending frequency
lowh2 = 2*fund - bin; %Second harmonic starting frequency
highh2 = 2*fund + bin; % Second harmonic ending frequency
lowh3 = 3*fund - bin; %H3 starting frequency
highh3 = 3*fund + bin; % H3 ending frequency
lowh4 = 4*fund - bin; %H4 starting frequency
highh4 = 4*fund + bin; %H4 ending frequency
[path] = uigetdir('C:\Users\Gowri\Documents\MATLAB\*.txt', 'select the folder with the ASCII files');
path = [path '\*.txt'];
files = dir(path);
nfiles = length(files);
randlist=1:1:nfiles;
files = char(files.name);
a = length(files);
FFTstart = FFTstart/1000;
FFTend = FFTend/1000;
%epoch details
for i= 1:nfiles
%number of points and timescale
name = files(i,:);
a = dlmread(name);
n = length(a);
fs = n*1000/epoch;
time = -prestim/1000 :1/fs: post/1000 -1/fs;
zerotime = -prestim/1000:1/fs:0-1/fs ;
%baseline correction ref: prestimulus
detrend = a - mean(a(1:size(zerotime)));
%%spectral analysis%%
%Time slice%
[trash position1] = min(abs(time-FFTstart));
[trash position2] = min(abs(time-FFTend));
sample = (a(position1:position2))';
%zero padding%
lin = fs;
win = round(fs*(5/1000));
window = hann(win);
env = window(1:round((length(window))/2));
vne = fliplr(env');
sample(1:length(env)) = env'.*sample(1:length(env));
sample(length(sample)-length(vne)+1:length(sample)) = vne.*sample(length(sample)-length(vne)+1:length(sample));
zero = zeros(1,round(fs-length(sample)));
abc = [sample zero];
%FFT
spect = abs(fft(abc));
amp=spect.*(2./length(sample));
amp = amp(1:round(round(fs)/2));
n=length(spect);
freq=fs/n.*(1:n);
f=freq(1:(round(n/2)));
%%H1
fstartL = find(f<=lowf0);
fstartL = fstartL(1,length(fstartL));
fstart = f(1,fstartL);
fendL = find(f>=highf0);
fendL = fendL(1,1);
fend = f(1,fendL);
range = f(fstartL:fendL);
f0 = amp(fstartL:fendL);
%find frequency with maximum amplitude
MAX = max(f0);
maxlocus = find(f0==MAX);
Fo = range(1,maxlocus);
H1 = mean(f0);
% H2
fstartL = find(f<=lowh2);
fstartL = fstartL(1,length(fstartL));
fstart = f(1,fstartL);
fendL = find(f>=highh2);
fendL = fendL(1,1);
fend = f(1,fendL);
range = f(fstartL:fendL);
h2 = amp(fstartL:fendL);
H2 = mean(h2);
%H3
fstartL = find(f<=lowh3);
fstartL = fstartL(1,length(fstartL));
fstart = f(1,fstartL);
fendL = find(f>=highh3);
fendL = fendL(1,1);
fend = f(1,fendL);
range = f(fstartL:fendL);
h3 = amp(fstartL:fendL);
H3 = mean(h3);
%H4
fstartL = find(f<=lowh4);
fstartL = fstartL(1,length(fstartL));
fstart = f(1,fstartL);
fendL = find(f>=highh4);
fendL = fendL(1,1);
fend = f(1,fendL);
range = f(fstartL:fendL);
h4 = amp(fstartL:fendL);
H4 = mean(h4);
Head = {'subject' 'F0' 'F0Amp' 'H2amp' 'H3amp' 'H4amp'};
M = {name Fo H1 H2 H3 H4 };
header = Head;
analysis(i+1,:) = M;
end
analysis(1,:)=header;
%xlswrite('baseline.txt', analysis)
2 Comments
Without the data files it is not possible to reproduce your error, but I'm guessing env is a vector in a different direction than you expected, leading to this:
[1 2].*[3;4]
And obviously that will not fit in two elements.
You should be aware that the apostrophe is the complex conjugate, not just a transpose. The difference is non-existent for real data, but since you're working with fft, complex data might show up. You need .' instead. You should also reconsider your use of length. It doesn't do what you might want it to do. Did you actually intend to have max(size(___))? You probably want to use numel instead.
Accepted Answer
More Answers (0)
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!