Skip to content

Commit

Permalink
add inverse DFT
Browse files Browse the repository at this point in the history
  • Loading branch information
KinWaiCheuk committed Sep 14, 2019
1 parent 85aeda4 commit 6e3109d
Show file tree
Hide file tree
Showing 6 changed files with 1,262 additions and 74 deletions.
144 changes: 142 additions & 2 deletions CQT.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import Spectrogram\n",
"from librosa.core import note_to_hz, cqt\n",
"from librosa import filters\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
Expand All @@ -16,7 +17,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -37,6 +38,145 @@
" + np.sin(2*np.pi*800*s, dtype=np.float32) + np.sin(2*np.pi*1600*s, dtype=np.float32) "
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"def cqt_filter_fft(sr, fmin, n_bins, bins_per_octave, tuning,\n",
" filter_scale, norm, sparsity, hop_length=None,\n",
" window='hann'):\n",
" '''Generate the frequency domain constant-Q filter basis.'''\n",
"\n",
" basis, lengths = filters.constant_q(sr,\n",
" fmin=fmin,\n",
" n_bins=n_bins,\n",
" bins_per_octave=bins_per_octave,\n",
" tuning=tuning,\n",
" filter_scale=filter_scale,\n",
" norm=norm,\n",
" pad_fft=True,\n",
" window=window)\n",
"\n",
" # Filters are padded up to the nearest integral power of 2\n",
" n_fft = basis.shape[1]\n",
"\n",
" if (hop_length is not None and\n",
" n_fft < 2.0**(1 + np.ceil(np.log2(hop_length)))):\n",
"\n",
" n_fft = int(2.0 ** (1 + np.ceil(np.log2(hop_length))))\n",
"\n",
" # re-normalize bases with respect to the FFT window length\n",
" basis *= lengths[:, np.newaxis] / float(n_fft)\n",
"\n",
" # FFT and retain only the non-negative frequencies\n",
" fft = get_fftlib()\n",
" fft_basis = fft.fft(basis, n=n_fft, axis=1)[:, :(n_fft // 2)+1]\n",
"\n",
" # sparsify the basis\n",
" fft_basis = util.sparsify_rows(fft_basis, quantile=sparsity)\n",
"\n",
" return fft_basis, n_fft, lengths, basis"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"sr = fs\n",
"fmin_t = fmin\n",
"n_filters = 12\n",
"bins_per_octave = 12\n",
"window= 'hann'\n",
"from librosa import get_fftlib\n",
"from librosa import util\n",
"fft_basis, n_fft, _, basis = cqt_filter_fft(sr, fmin_t,\n",
" n_filters,\n",
" bins_per_octave,\n",
" tuning=0,\n",
" filter_scale=1,\n",
" norm=None,\n",
" sparsity=0,\n",
" window=window)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.02515722+0.j , -0.01870174-0.01804653j,\n",
" -0.00143447-0.03432906j, ..., 0.02076852+0.04632163j,\n",
" -0.00143447+0.03432906j, -0.01870174+0.01804653j])"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.fft.fft(basis[0].real) + np.fft.fft(basis[0].imag)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.00081404+0.j , 0.00063303-0.01746888j,\n",
" 0.00015239-0.033281j , ..., -0.00045847+0.04500784j,\n",
" 0.00015239+0.033281j , 0.00063303+0.01746888j])"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x7fd8a0fa9ac8>]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAD8CAYAAAB6paOMAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALE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wbnPp5lcLR3+XzGEvi51udp4D3Hq73eWSxJnVvMU0OSpBfBIJCkzhkEktQ5g0CSOmcQSFLnDAJJ6pxBIEmdMwgkqXP/DuFepEEaYgwfAAAAAElFTkSuQmCC\n",
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.plot(fft_basis.toarray()[0].real)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading

0 comments on commit 6e3109d

Please sign in to comment.