Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

음성 스팩트로그램 수정내용, #54

Open
Kownby opened this issue May 17, 2018 · 0 comments
Open

음성 스팩트로그램 수정내용, #54

Kownby opened this issue May 17, 2018 · 0 comments
Assignees
Labels

Comments

@Kownby
Copy link
Collaborator

Kownby commented May 17, 2018

  • wav용 파일로 작성된 부분을 사용.

  • amplitude 가 sample 값 이란 부분을 db로 변경

  • 크기를 비율적으로 조절해서 사용하고 수치를 알수 있게 사용

  • 코드 내용과 내용수정으로 볼수 있도록 사용

  • 코드를 완전 바꾼거고 stackoverflow 에 있던 코드 사용 및 분석
    결과 사진.

  • 배고픔3
    spectrogram of 3 wav

  • 배고픔3 200 db 이상.
    spectrogram of 3 wav _bigsound

  • 아픔 3
    spectrogram of 3 wav

  • 아픔3 200 db 이상
    spectrogram of 3 wav _bigsound

  • 잠 1
    spectrogram of 1 wav

  • 잠 1 db 200 이상.
    spectrogram of 1 wav _bigsound

코드 부분

`

import numpy as np
from matplotlib import pyplot as plt
import scipy.io.wavfile as wav
from numpy.lib import stride_tricks
filepath = 's1' + '.wav'

short time fourier transform of audio signal

def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
#sig = signal frameSize = 2 ** 10

win = window(frameSize)
hopSize = int(frameSize - np.floor(overlapFac * frameSize))

# zeros at beginning (thus center of 1st window should be for sample nr. 0)
samples = np.append(np.zeros(int(np.floor(frameSize/2.0))), sig)
print(samples)
# cols for windowing
cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
# zeros at end (thus samples can be fully covered by frames)
samples = np.append(samples, np.zeros(frameSize))

frames = stride_tricks.as_strided(samples, shape=(int(cols), frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
frames *= win

return np.fft.rfft(frames)

scale frequency axis logarithmically

def logscale_spec(spec, sr=44100, factor=20.):
timebins, freqbins = np.shape(spec)

scale = np.linspace(0, 1, freqbins) ** factor
scale *= (freqbins-1)/max(scale)
scale = np.unique(np.round(scale))

# create spectrogram with new freq bins
newspec = np.complex128(np.zeros([timebins, len(scale)]))
for i in range(0, len(scale)):
    if i == len(scale)-1:
        newspec[:,i] = np.sum(spec[:,int(scale[i]):], axis=1)
    else:
        newspec[:,i] = np.sum(spec[:,int(scale[i]):int(scale[i+1])], axis=1)

# list center freq of bins
allfreqs = np.abs(np.fft.fftfreq(freqbins*2, 1./sr)[:freqbins+1])
freqs = []
for i in range(0, len(scale)):
    if i == len(scale)-1:
        freqs += [np.mean(allfreqs[int(scale[i]):])]
    else:
        freqs += [np.mean(allfreqs[int(scale[i]):int(scale[i+1])])]

return newspec, freqs

plot spectrogram

def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="jet"):
#sr , sample
samplerate, samples = wav.read(audiopath)

# s = stft
s = stft(samples, binsize)

print("lens",len(s[0]))


sshow, freq = logscale_spec(s, sr=samplerate, factor=1.0)

ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel
timebins, freqbins = np.shape(ims)
print( ((len(samples) / timebins) + (0.5 * binsize)) / samplerate )


print("timebins: {0} freqbins: {1}\n".format(timebins,freqbins))
#print(ims)
x = ((len(samples)/timebins)+(0.5*binsize))/samplerate
print (x * timebins)
plt.figure(figsize=(15, 7.5))
plt.imshow(np.transpose(ims), origin="lower", aspect="auto", cmap=colormap, interpolation="none")
plt.colorbar()

plt.xlabel("time (s)")
plt.ylabel("frequency (hz)")
plt.xlim([0, timebins-1])
plt.ylim([0, freqbins])

xlocs = np.float32(np.linspace(0, timebins-1, 5))
print(xlocs)
plt.xticks(xlocs, ["%.02f" % l for l in ((xlocs*len(samples)/timebins)+(0.5*binsize))/samplerate])
ylocs = np.int16(np.round(np.linspace(0, freqbins-1, 10)))
plt.yticks(ylocs, ["%.02f" % freq[i] for i in ylocs])
print(ylocs)

if plotpath:
    plt.savefig(plotpath, bbox_inches="tight")
else:
    plt.savefig("spectrogram of %r.png" % audiopath)
    plt.show()

plt.clf()

return ims

ims = plotstft(filepath)
`

@Kownby Kownby self-assigned this May 17, 2018
@Kownby Kownby added the info label May 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant