pythonpandasfftspectrogram

How do I calculate the spectogram of a a column which contains strings?


I have been trying to calculate the spectrogram of the output voltage values of an excel file in which the values are all stored automatically in the first column and it looks like this:

#CHANNEL:CH1
#CLOCK=200uS
#SIZE=130048
#UNITS:V

0.6902
0.7216
0.6902
0.6902
0.7216
0.7216
0.6902
0.6902
0.7216
0.6902
0.7216
0.6902
0.7216
0.7216
0.6902
0.6902
0.6902
0.6902
0.6902
0.6902
0.7216
0.7216
0.6902
...

So when I try the code below

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy import signal

data = pd.read_excel(r'/home/mirofeliciano/oscilo/test.xlsx')

x = np.array(data["#CHANNEL:CH1"])

voltage = x[4:]

fs = 0.05

period= 20

f, t, Sxx = signal.spectrogram(voltage, fs)

plt.pcolormesh(t, f, Sxx, shading='gouraud')

plt.ylabel('Frequency [Hz]')

plt.xlabel('Time [sec]')

plt.show()

It gives the error:

TypeError: Image data of dtype object cannot be converted to float

Which, indeed when I look at the voltage vector says

In [2]: voltage
Out[2]: array([0.6902, 0.7216, 0.6902, ..., 0.6902, 0.6902, 0.7216], dtype=object)

So even though I'm cutting the vector to not include the first few rows of strings it is still considering it as an object. So my question would be how to obtain only the values in a way that it's considering only the numbers.


Solution

  • you can convert the entries in voltage like so:

    f, t, Sxx = signal.spectrogram(voltage.astype(float), fs)