31 lines
1.2 KiB
Plaintext
Executable File
31 lines
1.2 KiB
Plaintext
Executable File
|
|
from mathutils import fft, get_modulus
|
|
import random
|
|
|
|
def plot_spectrum(signal, sampling_frequency, plots = None):
|
|
number_of_samples = len(signal)
|
|
time_vector = [x / sampling_frequency for x in frange(0, number_of_samples, 1)]
|
|
tranform = fft(signal)
|
|
two_side_spectrum = [x / number_of_samples for x in get_modulus(tranform)]
|
|
spectrum = [two_side_spectrum[0],] + [x * 2 for x in two_side_spectrum[1:len(two_side_spectrum)/2 + 1] ]
|
|
number_of_samples = len(tranform) # Signal may have been padded to next power of two
|
|
freq_vector = [x * sampling_frequency / float(number_of_samples) for x in frange(0, len(spectrum) , 1)]
|
|
if plots is None:
|
|
plots = plot([signal,spectrum], ["signal", "spectrum"],[time_vector, freq_vector], context = "Spectrum")
|
|
else:
|
|
plots[0].getSeries(0).setData(time_vector,signal)
|
|
plots[1].getSeries(0).setData(freq_vector,spectrum)
|
|
return plots
|
|
|
|
def get_sample(samples, sampling_freq):
|
|
return map(lambda t:math.sin(100*2*math.pi*t/sampling_freq) + random.random(), frange(0,samples,1))
|
|
|
|
|
|
|
|
plots = None
|
|
while(True):
|
|
signal = get_sample(1024, 1024.0)
|
|
plots = plot_spectrum(signal,sampling_freq, plots)
|
|
time.sleep(0.1)
|
|
|