I am trying to represent sensor data in spectrogram form.

The data set consists of multiple 1D time series with a constant frequency of 1024 Hz taken from observations, cut into 1-minute sequences. Therefore, Each data frame has 61440 columns corresponding to all time steps contained in one minute at 1024Hz.

what I want to achieve is to split the signal into chunks (512 time steps each) and create a spectrogram image with shape(64,64,3) for that specific time interval. I don't have a background in signal processing beforehand. As I have observed so far spectrograms represent the signal in Freq. , time axis along with altitude as pixel intensities. Is it possible to achieve my goal?

what I tried so far gave me results with the (freq-interval,data-points) shape of an image e.g. (30,512).

My real aim is to represent a signal as an image without losing features. I will then use those inputs in a classification task. I am open to any suggestions.



2 Answers 2


A (visual) spectrogram represents the magnitude of the DFT. To retain information you generally need to keep the complex DFT output. Or find some real-valued perfect reconstruction transform (MDCT?)

A spectrogram is made by sliding a window along the input vector. The size of that window determines the frequency dimension of the output, while the input vector size and the sliding window hop size determines the other dimension of the output.

edit: in MATLAB-ese

x = [randn(571,1)]; 
y = spectrogram(x, 127, 120, 127); 
ans =

    64    64

Or if you want explicit control you might do something like:

x = [randn(512,1)];
wsize = 127;
wolap = 119;
x_buf = buffer(x, wsize, wolap); 
x_buf = x_buf .* hann(wsize);
X = fft(x_buf);
X = X(1:(end+1)/2,:);
M = abs(X);
ans =

    64    64

Note that things like window size, window and hop size will affect the properties of the spectrogram, likely affecting the "image-like" statistics that you seemingly want to exploit for machine-learning.


  • $\begingroup$ So, can I have an output of a square image by adjusting the sliding window hop size of the spectrogram? $\endgroup$ Aug 21, 2021 at 8:17

Yes, just adjust hop_size and n_fft such that width matches height. But mind:

  1. hop_size <= window_length must hold to not lose information (NOLA)
  2. (width, height, 3) can't be done with spectrogram, at most , 2) if going with complex, or , 1) if absolute value (so treat as grayscale for latter)
  3. Will lose information no matter what with hop_size > 1 if going for absolute value, but not necessarily much

Control image size via n_fft (height), and adjust hop_size (width) accordingly.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.