I have a 1 Dimensional signal (EEG signal) and I want to convert it to a 2D signal in order to use it as input for a CNN model. Can I use a Fourier transform to do that?
Asked
Active
Viewed 2,476 times
2
-
Hi, try giving more details about your problem, what you have tried, and the desired input/output – olirwin Aug 06 '20 at 14:13
-
Try to build a CNN that takes a 1D input. You’ll be much better off than randomly adding a new, meaningless dimension. – Cris Luengo Aug 06 '20 at 23:01
-
@CrisLuengo I tried but accuracy was not good enough, I thought about using a 2d input – wassidi Aug 08 '20 at 12:09
-
I support the Statement of Cris Luengo. Use a 1D Conv Layer. If you have a bad accuracy you should maybe raise your model complexity by adding additional conv layers or anything similar. – Sebastian Baum Aug 08 '20 at 21:51
2 Answers
1
You're probably looking for a "spectrogram": https://en.wikipedia.org/wiki/Spectrogram This is a 2D plot of amplitude vs time and frequency.
You make it by applying a short-time Fourier transform to successive overlapping portions of your signal.
Scipy has methods that will do this for you:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.stft.html https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.spectrogram.html
The number of frequencies is proportional to the window size (nperseg in the above docs). You need to determine the appropriate frequency resolution for EEGs in order to set this.

Matt Timmermans
- 53,709
- 3
- 46
- 87
-
The examples in the documentation add some noise to the original signal, does this affects the accuracy of the model?? – Alaa May 22 '22 at 18:49
-
1Certainly it would affect the accuracy of the model. I'm not sure exactly how, though. If you're training a CNN, you may want to add some noise to prevent overtraining on minute details of the input. – Matt Timmermans May 22 '22 at 19:55
0
In here x instead of np.linspace use your data points (1D) array. Your 2D will be made up of (x,freq)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 5, 100)
y = np.sin(2 * np.pi * x)
## fourier transform
f = np.fft.fft(y)
## sample frequencies
freq = np.fft.fftfreq(len(y))
plt.plot(freq, abs(f) ** 2)
plt.show()
print( "Shape of singal array : {}".format(x.shape))
print( "Shape of frequency array : {}".format(freq.shape))
new_2D_array = np.vstack((x, freq)).T
print(new_2D_array)

Andres Ordorica
- 302
- 1
- 5
-
what do you mean by data points ? (my input signal is EEG medical signal) – wassidi Aug 06 '20 at 22:03
-
Sorry I am not very aware of the medical terms, but your EEG signal is a 1D array right ? If so, then perform the fourier transform to get the frecuency, thus you get signal+frecuency . – Andres Ordorica Aug 06 '20 at 22:19
-
Check the edit I made. When I mean data points , I mean your 1D array made up of the values from your EEG . You perform fourier transform on those points . Then you combine original EEG array + frecuency array, making a 2D array. – Andres Ordorica Aug 06 '20 at 22:24
-
Even though EEG is medical it needs to be made up from values, I would think of amplitude or other. so you get EEG_signal = [1,2,3,4,5,etc] -> 1D , the new array new_2d_array = [[1,F(1)], [2,F(2)] ...] hence 2D. – Andres Ordorica Aug 06 '20 at 22:28
-
I wouldn’t want to do this, as the new dimension has no spatial relationship. OP wants to use a CNN, what does it mean to average f(i) and y(i) together? – Cris Luengo Aug 06 '20 at 23:00
-
@CrisLuengo Thank you , one last question ! you think it would be better to use matlab or python ? – wassidi Aug 07 '20 at 22:34
-
@wassimdiai: Use whatever language and whatever tool you're most comfortable with. "Better" is a subjective evaluation anyway. – Cris Luengo Aug 07 '20 at 22:37