Skip to content

Instantly share code, notes, and snippets.

@jniemann66
Last active November 26, 2017 04:15
Show Gist options
  • Save jniemann66/718834a76e47ee24fed94b7d7bb1c372 to your computer and use it in GitHub Desktop.
Save jniemann66/718834a76e47ee24fed94b7d7bb1c372 to your computer and use it in GitHub Desktop.
show impulse response as spectrum (no windowing)
# -*- coding: utf-8 -*-
"""
Read a 32-bit (!) wave file and plot frequency response
"""
import numpy as np
import matplotlib.pyplot as plt
import wave
import sys
import os
np.set_printoptions(precision=15, suppress=True)
def freqresponse(b, fs, N):
Y = np.fft.fft(b, N)
Ym = np.abs(Y)
Ydb = 20*np.log10(Ym/Ym.max())
# Frequency vector
f = np.arange(N)*fs/N
return Ydb, f
if __name__ == "__main__":
# filename manipulation
inputPath = sys.argv[1]
dirname, filename = os.path.split(inputPath)
outfilename = os.path.splitext(filename)[0] + '.png'
outputPath = os.path.join(dirname, outfilename)
# analyze wav file
wavdata = wave.open(inputPath,'r')
fs = wavdata.getframerate()
N = wavdata.getnframes()
signal = wavdata.readframes(N)
signal = np.fromstring(signal, 'Int32')
Ydb, f = freqresponse(signal, fs, N)
#Ydb = Ydb * np.blackman(N)
# Plot the frequency response
plt.clf()
plt.plot(f, Ydb, 'g')
plt.grid(True)
plt.hold(True)
plt.title(filename)
plt.xlabel('Frequency [Hz]')
plt.ylabel('Amplitude [dB]')
plt.xlim(0, 0.5*fs)
plt.ylim(-210,10)
plt.legend()
plt.savefig(outputPath, bbox_inches='tight')
plt.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment