1

I have two lists of float values, one for time and other for voltage values taken from an oscilloscope (I assume). I have to draw an amplitude spectrum plot, but i'm not exactly sure what function I need to use and what parameters I need to give it, I tried fft(u), but it didn't work.

Any help is appreciated, let me know if you need more info.

James K
  • 3,692
  • 1
  • 28
  • 36
Pandryl
  • 33
  • 1
  • 1
  • 6
  • Take a look at the `matplotlib` library for plotting in Python. – leekaiinthesky May 17 '15 at 09:43
  • I'm using IDLE in pylab as far as including specific libraries is concerned i think i'm good. I couldn't do fft, thats the problem. I get error 'module' object is not callable – Pandryl May 17 '15 at 09:52
  • I used magnitude_spectrum(insertvaluehere) in case somebody else has the same problem, honestly i'm not sure its completely correct, but its something. Thanks for the help leekaiinthesky! – Pandryl May 17 '15 at 10:23
  • This question was already answer before: https://stackoverflow.com/questions/24382832/audio-spectrum-extraction-from-audio-file-by-python – Jose R. Zapata Apr 07 '20 at 14:30

2 Answers2

7

Use numpy.

As an example, let me show how I analysed the frequencies in a stereo WAV file;

First I read the data and separated it in the left and right channels;

import wave
import numpy as np

wr = wave.open('input.wav', 'r')
sz = 44100 # Read and process 1 second at a time.
da = np.fromstring(wr.readframes(sz), dtype=np.int16)
left, right = da[0::2], da[1::2]

Next I run a discrete fourier transform on it;

lf, rf = abs(np.fft.rfft(left)), abs(np.fft.rfft(right))

And we plot the left channel with mathplotlib;

import matplotlib.pyplot as plt

plt.figure(1)
a = plt.subplot(211)
r = 2**16/2
a.set_ylim([-r, r])
a.set_xlabel('time [s]')
a.set_ylabel('sample value [-]')
x = np.arange(44100)/44100
plt.plot(x, left)
b = plt.subplot(212)
b.set_xscale('log')
b.set_xlabel('frequency [Hz]')
b.set_ylabel('|amplitude|')
plt.plot(lf)
plt.savefig('sample-graph.png')

The graph looks something like this;

Community
  • 1
  • 1
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • Thank you very much it was very helpful. abs(fft.fft()) worked well, i just didn't know what i should use :D – Pandryl May 17 '15 at 15:08
0

Here is the Frequency-Time spectrum of the signal, stored in the wave file

import wave
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

signal_wave = wave.open('voice.wav', 'r')
sample_frequency = 16000

data = np.fromstring(signal_wave.readframes(sample_frequency), dtype=np.int16)
sig = signal_wave.readframes(-1)

sig = np.fromstring(sig, 'Int16')

For the wave file

sig = sig[:]

For some segment of the wave file

sig = sig[25000:32000]

To plot spectrum of the signal wave file

plt.figure(1)
c = plt.subplot(211)
Pxx, freqs, bins, im = c.specgram(sig, NFFT=1024, Fs=16000, noverlap=900)
c.set_xlabel('Time')
c.set_ylabel('Frequency')
plt.show()

graph example

4levels
  • 3,134
  • 1
  • 23
  • 22
Nikhil Parashar
  • 431
  • 5
  • 11