Professional Documents
Culture Documents
Matlab Code To Estimate The Power Spectrum of The EEG Signal
Matlab Code To Estimate The Power Spectrum of The EEG Signal
Matlab Code To Estimate The Power Spectrum of The EEG Signal
fs = 3000 % fs
Sampling frequency, positive scalar. Sampling frequency, specifie
d as a positive scalar. The sampling frequency is the number of samples per unit
time. If the unit of time is seconds, the sampling frequency has units of hertz
.
T = 1/fs;% sampling rate or frequency
load('J:\BIOM_Signal_processing\Hw9\EEGsignal_1') % contains eeg1 and fs
N =length(eeg1); ls = size(eeg1); % find the length of the data per second
tx =[0:length(eeg1)-1]/fs;% Make time axis for EEG signal
figure; subplot (211), plot(tx,eeg1); xlabel('Time (s)'), ylabel('Amplitude (mV)
'), title('Original EEG signal'); %EEG waveform
subplot(212), plot(tx,eeg1);
%fignum = fignum + 1; figure(fignum);
xlabel('Time (s)'), ylabel('Amplitude (mV)'), title('Zoom into original EEG sign
al at 1 to 2 seconds'), xlim([1,2]) % Used to zoom in on single ECG waveform
figure;
NFFT = 2 ^ nextpow2(N); %% number points. Number of DFT points, specified as a
positive integer. For a real-valued input signal, x, the PSD estimate, pxx has l
ength (nfft/2 + 1) if nfft is even, and (nfft + 1)/2 if nfft is odd. For a compl
ex-valued input signal,x, the PSD estimate always has length nfft. If nfft is sp
ecified as empty, the default nfft is used.
Y = fft(eeg1, NFFT)/N; %% fft of the EEG signals
f = (fs/2 * linspace(0,1,NFFT/2+1))'; % Vector containing frequencies in Hz
amp =( 2 * abs(Y(1: NFFT/2+1))); % Vector containing corresponding amplitudes
subplot(2,1,1), plot (f, amp);title ('plot single-sided amplitude spectrume of t
he EEG signal'); xlabel ('frequency (Hz)'); ylabel ('|y(f)|');grid on;
%Estimate the power spectrum of the 10-s epoch by computing the periodogram
%the plot using periodogram with no outputs.
periodogram_1 = periodogram(eeg1);
subplot(2,1,2), plot (f,periodogram_1);title('Periodogram power spectral Densist
y Estimate of the EEG signal'); xlabel ('frequency (Hz)');ylabel ('Power/Frequen
cy(dB)');grid on;
%%% plot the periodogram with the length of EEG signal
nfft= length(eeg1);
periodogram_2 =periodogram(eeg1,[],nfft);
f1 = (fs/2 * linspace(0,1,nfft/2+1))'; % Vector containing frequencies in Hz
figure;
subplot(2,2,1), plot(f1,periodogram_2);title('Periodogram power spectral Densist
y Estimate of the EEG signal and length of EGG signal'); xlabel('frequency (Hz)'
);ylabel('Power/Frequency(dB)');grid on;
%%%%The signal is 30001 samples in length. Obtain the periodogram using the defa
ult rectangular window and DFT length. The DFT length is the next power of two g
reater than the signal length, or 32768 points.
%Because the signal is real-valued and has even length, the periodogram is one-s
ided and there are 512/2+1 points.
[pxx,w] = periodogram(eeg1);
subplot(2,2,2), plot(w,10*log10(pxx)); title ('Periodogram using the default rec
tangular window and DFT length'); xlabel ('frequency(Hz)'); ylabel ('Power/Frequ
ency(dB)');grid on;
%%Modified Periodogram with Hamming Window. Obtain the modified periodogram of a
n input EEG signal with no noise. The signal is 30001 samples in length. Obtain
the modified periodogram using a Hamming window and default DFT length. The DFT
length is the next power of two greater than the signal length, or 32786 points
.
%Because the signal is real-valued and has even length, the periodogram is one-s
ided and there are 32768/2+1 points.
hamming_1=periodogram(eeg1,hamming(length(eeg1)));
subplot(2,2,3), plot (f,hamming_1);title ('Periodogram using the hamming window
0,40]);grid on;
%text(0.5,-1/3,'{\itNote the odd symmetry.}')
%%%%%%%
%%% problem 2
%2. Given three signals:
%x1 = cos(2*pi*4*t)
%x2 = cos(2*pi*20*t)
%x3 = cos(2*pi*40*t)
%Generate .3 seconds of each signal. First, combine them into new signals
%x4 = x1 + x2 + x3
x4 is the sum of the first three signals.
%x5 = [x1 x2 x3] where x5 is concatenated from the first 3 vectors
clc;clear all;close all;
t = 0:0.01:1;
x1 = cos(2*pi*4*t); % for x1: frequency is 4hz,
x2 = cos(2*pi*20*t);%x2: frequency = 20Hz,
x3 = cos(2*pi*40*t);% is 40 Hz.
x4 = x1 + x2 + x3;
f1 = 4;
f2 = 20;
f3= 40;
T1 = 1/f1;
T2 = 1/f2;
T3 = 1/f3;
figure;
subplot(2,2,1), plot(t,x1); xlabel('Time (s)'), ylabel('Amplitude'), title('sign
al: x1 = cos(2*pi*4*t) ');
subplot(2,2,2), plot(t,x2); xlabel('Time (s)'), ylabel('Amplitude'), title('sign
al: x2 = cos(2*pi*20*t) ');
subplot(2,2,3), plot(t,x3); xlabel('Time (s)'), ylabel('Amplitude'), title('sign
al: x3 = cos(2*pi*40*t) ');
subplot(2,2,4), plot(t,x4); xlabel('Time (s)'), ylabel('Amplitude'), title('sign
al: x4 = x1 + x2 + x3 ');
% Concatinate the three signals:
x5 = [x1 x2 x3] ;
figure;
plot(x5); xlabel('Time (s)'), ylabel('Amplitude'), title('Concatinate the thr
ee signals: x1, x2, and x3 ');
%What is the Nyquist rate for this signal?
fN1=f1/2;
fN2 = f2/2;
FN3= f3/2; %Fmax
fs = 50; %The Nyquist rate is FN= 2Fmax
%plot the spectrum of each signal. Compare the results.
NFFT_x1 = 2 ^ nextpow2(length(x1)); %% number points. Number of DFT points, spe
cified as a positive integer. For a real-valued input signal, x, the PSD estimat
e, pxx has length (nfft/2 + 1) if nfft is even, and (nfft + 1)/2 if nfft is odd.
For a complex-valued input signal,x, the PSD estimate always has length nfft. I
f nfft is specified as empty, the default nfft is used.
Y_x1 = fft(x1, NFFT_x1)/length(x1); %% fft of the EEG signals
f_x1 = (fs/2 * linspace(0,1,NFFT_x1/2+1))'; % Vector containing frequencies in H
z
amp_x1 =( 2 * abs(Y_x1(1: NFFT_x1/2+1))); % Vector containing corresponding ampl
itudes
NFFT_x2 = 2 ^ nextpow2(length(x2)); %% number points. Number of DFT points, spe
cified as a positive integer. For a real-valued input signal, x, the PSD estimat
e, pxx
For a
f nfft
Y_x2 =
f_x2 =
z
amp_x2
itudes
has length (nfft/2 + 1) if nfft is even, and (nfft + 1)/2 if nfft is odd.
complex-valued input signal,x, the PSD estimate always has length nfft. I
is specified as empty, the default nfft is used.
fft(x2, NFFT_x1)/length(x2); %% fft of the EEG signals
(fs/2 * linspace(0,1,NFFT_x2/2+1))'; % Vector containing frequencies in H
=( 2 * abs(Y_x2(1: NFFT_x2/2+1))); % Vector containing corresponding ampl