# How to identify and differentiate frequency and time in EEG data using python?

I have an EEG data of 200 Hz and sampled at 4097. I have few doubts and questions related to this data:

1. What does it mean by sampling in EEG data?
2. I am getting the sample rate and the frequency when I plot the data as shown below. Shouldn't it be the time and frequency?

I don't understand what I'm missing here. Can anyone explain how am I supposed to identify the time and frequency of EEG data?

EEG data file : http://filesave.me/file/123174/eeg-txt.html

Code for the above plot:

x = np.array([])
input = open('eeg.txt', 'r')
for file in input:
file = file.replace(',','')
x = np.append(x, float(file))
input.close()
plt.subplot(1, 1, 1)
plt.plot(x)
plt.ylabel('EEG signal')
plt.show()


FFT calculation:

import numpy as np
x = np.array([])
input = open('eeg.txt', 'r')
for file in input:
file = file.replace(',','')
x = np.append(x, float(file))
input.close()
plt.subplot(1, 1, 1)
ff = fft(x)
plt.plot(ff)
plt.ylabel('EEG signal')
plt.show()


Wavelet decomposition:

coeffs = wavedec(x, 'db4', level=6)
cA2, cD1, cD2,cD3,cD4,cD5,cD6 = coeffs

plt.subplot(7, 1, 1)
plt.plot(x)
plt.ylabel('Noisy Signal')
plt.subplot(7, 1, 2)
plt.plot(cD6)
plt.ylabel('noisy')
plt.subplot(7,1,3)
plt.plot(cD5)
plt.ylabel("gamma")
plt.subplot(7,1,4)
plt.plot(cD4)
plt.ylabel("beta")
plt.subplot(7,1,5)
plt.plot(cD3)
plt.ylabel("alpha")
plt.subplot(7,1,6)
plt.plot(cD2)
plt.ylabel("theta")
plt.subplot(7,1,7)
plt.plot(cD1)
plt.ylabel("delta")
plt.draw()
plt.show()


Results of wavelet:

• What did you plot in your code, the raw data or some function of the raw data ? – Gilles Mar 1 '16 at 10:31
• Thank you for the reply :) I edited my post with the code I used to plot this signal. How do I analyze and identify time and frequency of this signal? – pythongeek36 Mar 1 '16 at 11:01
• From your code you're plotting your data in raw form i.e. a sequence of samples; and that's still (discrete) time domain I think. If you want to analyze the frequency content of the signal, try the Discrete Fourier Transform, and then look for the Fast Fourier Transform function fft in Python. – Gilles Mar 1 '16 at 12:02
• I updated my post with FFT function.Also calulcating FFT would give me only the frequency. – pythongeek36 Mar 1 '16 at 12:28

What does it mean by sampling in EEG data?

Think of it this way. Some true EEG signal exists, but when you 'sample' you are grabbing little snippets of that signal to try and reconstruct it the way it really is. That signal has a frequency spectrum that looks like this

You must sample at a rate called "nyquist" to properly reconstruct the signal. That means at twice the highest frequency in that signal.

Also note that spike at 60 Hz. That probably isn't from the EEG but rather your power lines. Where are you located? 60 hz noise is very common in eeg/ekg signals. filter that out.

I am getting the sample rate and the frequency when I plot the data as shown below. Shouldn't it be the time and frequency?

Generally the signal frequency is plotted against the amplitude or power of the signal if you are analyzing the frequency spectrum (as shown). On the other hand, you might want to see the signal in the "time domain". What you probably have is the "sample number" without any reference to time, but if you know the sampling rate then you can convert the x-axis to time.

samples = 1:4097;    % 4097 samples
Fs = 200;            % Sampling Frequency (Hz)
t = samples/Fs;      % Time Vector (seconds)


• Thank you very much. Your answer is so useful. Could you please show me the code of plotting time series in python? So should the X axis be "t = samples/Fs; " and y axis should be Fs? – pythongeek36 May 22 '16 at 14:50
• I haven't used this in python, but there are several libraries you could use. The x-axis is time as shown is t=samples/Fs. Since you know how often you "sampled" that's how you convert to time. If you sample every 1 second, then each datapoint is 1 second. In your case you sampled 200 hz which is 1/200=5 milliseconds. The Y-axis is actually a voltage, not Fs. The sensors that we use to gather data produce tiny voltages (usually millivolts) which are then amplified by the circuit. . This might be useful: [matplotlib] (pybytes.com/pywfdb/example-drawing.html) – jmaturner May 22 '16 at 15:06
• Thank you for your explanation. I will try according to the link you sent :) – pythongeek36 May 25 '16 at 3:08

It is hard to answer your question, since you do not seem to have experience with EEG data and/or general signal processing.

You say, your data is sampled at 200 Hz, which seems good to me for EEG data.

1. What does it mean by sampling in EEG data? That means, that the (continous) EEG signal is choped up into discrete values. This is always necessary for further signal processing with computers.

2. I am getting the sample rate and the frequency when I plot the data as shown below. Shouldn't it be the time and frequency?

I don't know what you mean by I am getting the sample rate and the frequency when I plot the data. You certainly don't. The sampling rate/frequency is a fixed number given by your experimental hardware. You don't get it from your data - you have to know it from your experiment to get meaningful results from your data.

I also don't know what Shouldn't it be the time and frequency? means. Your EEG data is certainly a voltage signal over time, hence you have a signal in time domain.

Your sampling rate is given from your experimental settings and is 200 Hz as you say. Hence, each time point is separated by $$\Delta t = \frac{1}{f} = \frac{1}{200\,\textrm{Hz}} = 5\,\textrm{ms}$$

Your total signal that you have plotted is therefore $$4097 \times 5\times 10^{-3}\textrm{s} = 20.485\,\textrm{s}$$ long.

That is what you know from your data. Any further data extraction is up to you or your research question. If you'd like to get the frequencies that are in the signal, try to Fourier-transform it and extract the frequency region that you are interested in. As I see it, you have some apparent frequency components at approx. 13.4 Hz, 57.7 Hz, 81.0 Hz, and 88.0 Hz. Probably you are only interested in the lower spectrum around 13.4 Hz...

• Thank you for a well detailed answer. I am sorry that my question is not clear enough. What I actually meant was , I am getting the sample rate as the x axis and frequency as the y axis when I plot the raw signal. Whereas, I normally see these EEG plots as time and frequency in research papers. I assume that it's done by few calculations and functions as you have mentioned. Also my main aim is to decompose the signal into EEG bands and analyze the signal using wavelet transform. – pythongeek36 Mar 1 '16 at 13:03
• I have updated my question with the wavelet decomposition I tried. It can be seen that it's decomposing the sample rate, but not the frequency. I couldn't figure out what's wrong in this code – pythongeek36 Mar 1 '16 at 13:07
• You are welcome. Your comment however does not clear things up: Getting the sampling rate on the x-axis does not make sense. Your sampling rate is 200 Hz - a single number! Make very sure what your hardware gives you - from your statement on the x-axis being the sampling rate, I doubt that your hardware gives you any frequencies as a signal. – M529 Mar 1 '16 at 13:11
• On your wavelet decomposition: Make sure that you understand what you are doing. Just applying a transformation because its output "looks like something some papers do" will lead you in serious trouble. You have to understand what you want to extract from your data and then apply the correct data analysis. It is not the other way around! And, sadly, I assume you caught that trap. – M529 Mar 1 '16 at 13:17
• I would like to extract the EEG bands such delta(1-3 Hz),theta(4-7 Hz) etc. Can u please explain with some sample codes or provide me a solution to my wavelet code? – pythongeek36 Mar 1 '16 at 13:29