GithubHelp home page GithubHelp logo

shaun95 / pytorch-audio_feature Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wanghelin1997/pytorch-audio_feature

0.0 1.0 0.0 15 KB

Audio feature extraction in Pytorch module.

Python 100.00%

pytorch-audio_feature's Introduction

Pytorch-audio_feature

Audio feature extraction in Pytorch module.

To test melspectrogram

import torch
from audio_feature import STFT
from audio_feature import melspectrogram
import numpy as np
import librosa 
import matplotlib.pyplot as plt
# from spec import Melspectrogram

audio = librosa.load(librosa.util.example_audio_file(),sr=16000)[0]
audio = audio[:16000]
device = 'cuda'
filter_length = 1024
hop_length = 512
win_length = 1024 # doesn't need to be specified. if not specified, it's the same as filter_length
window = 'hann'
librosa_stft = librosa.stft(audio, n_fft=filter_length, hop_length=hop_length, window=window)
_magnitude = np.abs(librosa_stft)

y = audio
D = np.abs(librosa.stft(y))**2
S = librosa.feature.melspectrogram(S=D,n_mels = 128,sr=16000)
# melspec = librosa.feature.melspectrogram(_magnitude**2, sr=44100,
#                                     n_fft=1024, 
#                                     hop_length=512, 
#                                     n_mels = 128) 

audio = torch.FloatTensor(audio)
audio = audio.unsqueeze(0)
audio = audio.to(device)

stft = STFT(
    filter_length=filter_length, 
    hop_length=hop_length, 
    win_length=win_length,
    window=window
).to(device)
# spect = Melspectrogram(hop=512, n_fft=1024, window=None, sr=44100).to(device)
# ss = spect.forward(audio.unsqueeze(0))
spect = melspectrogram(n_fft=1024, sr=16000).to(device)
magnitude, phase = stft.transform(audio)
ss = spect.forward(magnitude.to(device))
# ss = librosa.feature.melspectrogram(S=magnitude.squeeze(0).to('cpu'),n_fft=1024,sr=44100)
# print(ss.shape)
plt.figure(figsize=(6, 6))
plt.subplot(211)
plt.title('PyTorch STFT magnitude')
plt.xlabel('Frames')
plt.ylabel('FFT bin')
# plt.imshow(20*np.log10(1+magnitude[0].cpu().data.numpy()), aspect='auto', origin='lower')
plt.imshow(20*np.log10(1+ss[0].cpu().data.numpy()), aspect='auto', origin='lower')
plt.subplot(212)
plt.title('Librosa STFT magnitude')
plt.xlabel('Frames')
plt.ylabel('FFT bin')
# plt.imshow(20*np.log10(1+_magnitude), aspect='auto', origin='lower')
plt.imshow(20*np.log10(1+S), aspect='auto', origin='lower')
plt.tight_layout()
plt.savefig('images/stft.png')


pytorch-audio_feature's People

Contributors

wanghelin1997 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.