domingo, 8 de noviembre de 2020

Python Spectrogram for 1-second Sound


http://raden.fke.utm.my/blog/pythonspectrogramfor1-secondsound

Python Spectrogram for 1-second Sound


 First install pydub. Use the virtual environment and Anaconda for best results.


$ ls anaconda3/envs
$ source activate sci
$ conda install -c conda-forge pydub

Launch spyder and enter the following code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""

"""


#import the pyplot and wavfile modules
import matplotlib.pyplot as plot
from scipy.io import wavfile
from os import path
from pydub import AudioSegment

# Read the wav file (mono)
src = '/home/munim/Downloads/tailorbird1.mp3'
dst = 'tmp.wav'

sound = AudioSegment.from_mp3(src)
sound = sound.set_channels(1)
#onesecond = sound[-1000:] # last one second
onesecond = sound[:1000] # first one second

onesecond.export(dst, format='wav')
samplingFrequency, signalData = wavfile.read(dst)


print(samplingFrequency)
# Plot the signal read from wav file
plot.subplot(211)
plot.title('Spectrogram of file {}'.format(src))

plot.plot(signalData)
plot.xlabel('Sample')
plot.ylabel('Amplitude')
plot.subplot(212)
plot.specgram(signalData,Fs=samplingFrequency)
plot.xlabel('Time')
plot.ylabel('Frequency')
plot.show()


Here's the output:


References:

No hay comentarios:

Publicar un comentario