|
11 | 11 | "%autoreload 2\n",
|
12 | 12 | "\n",
|
13 | 13 | "import sys\n",
|
14 |
| - "sys.path.insert(0, 'benchmarks/iir/')" |
| 14 | + "#sys.path.insert(0, '.benchmarks/iir/')" |
15 | 15 | ]
|
16 | 16 | },
|
17 | 17 | {
|
18 | 18 | "cell_type": "code",
|
19 |
| - "execution_count": 42, |
| 19 | + "execution_count": 2, |
20 | 20 | "id": "10e5b8fc-6972-4502-a397-300a800df9ee",
|
21 | 21 | "metadata": {},
|
22 | 22 | "outputs": [],
|
|
27 | 27 | "from matplotlib import pyplot as plt\n",
|
28 | 28 | "\n",
|
29 | 29 | "import numpy\n",
|
30 |
| - "import pandas" |
| 30 | + "import pandas\n", |
| 31 | + "import librosa\n", |
| 32 | + "import seaborn" |
31 | 33 | ]
|
32 | 34 | },
|
33 | 35 | {
|
34 | 36 | "cell_type": "code",
|
35 |
| - "execution_count": 43, |
| 37 | + "execution_count": 30, |
36 | 38 | "id": "fe122a40-66ed-4add-b59a-3254a35ece9a",
|
37 | 39 | "metadata": {},
|
38 | 40 | "outputs": [
|
|
54 | 56 | "name": "stderr",
|
55 | 57 | "output_type": "stream",
|
56 | 58 | "text": [
|
57 |
| - "/tmp/ipykernel_12537/4127207659.py:11: RuntimeWarning: divide by zero encountered in log10\n", |
| 59 | + "/tmp/ipykernel_27591/4127207659.py:11: RuntimeWarning: divide by zero encountered in log10\n", |
58 | 60 | " db = 20 * numpy.log10(abs(h))\n"
|
59 | 61 | ]
|
60 | 62 | },
|
|
118 | 120 | },
|
119 | 121 | {
|
120 | 122 | "cell_type": "code",
|
121 |
| - "execution_count": 53, |
| 123 | + "execution_count": 5, |
122 | 124 | "id": "0dff57e5-69cf-4ad7-80f0-d4b721a77910",
|
123 | 125 | "metadata": {},
|
124 | 126 | "outputs": [
|
125 | 127 | {
|
126 | 128 | "data": {
|
127 | 129 | "text/plain": [
|
128 |
| - "array('i', [29321, 0, -58643, 29321, -58280, 26239, 2210, 0, 4420, 2210, -37452, 13526])" |
| 130 | + "array('i', [26233, 0, -52466, 26233, -51149, 21015])" |
129 | 131 | ]
|
130 | 132 | },
|
131 |
| - "execution_count": 53, |
| 133 | + "execution_count": 5, |
132 | 134 | "metadata": {},
|
133 | 135 | "output_type": "execute_result"
|
134 | 136 | }
|
|
166 | 168 | },
|
167 | 169 | {
|
168 | 170 | "cell_type": "code",
|
169 |
| - "execution_count": 48, |
| 171 | + "execution_count": 6, |
170 | 172 | "id": "7857c33a-6f7f-405b-9d01-16e9d86043f3",
|
171 | 173 | "metadata": {},
|
172 | 174 | "outputs": [
|
|
184 | 186 | "name": "stderr",
|
185 | 187 | "output_type": "stream",
|
186 | 188 | "text": [
|
187 |
| - "/tmp/ipykernel_12537/4127207659.py:11: RuntimeWarning: divide by zero encountered in log10\n", |
| 189 | + "/tmp/ipykernel_27591/4127207659.py:11: RuntimeWarning: divide by zero encountered in log10\n", |
188 | 190 | " db = 20 * numpy.log10(abs(h))\n"
|
189 | 191 | ]
|
190 | 192 | },
|
|
222 | 224 | },
|
223 | 225 | {
|
224 | 226 | "cell_type": "code",
|
225 |
| - "execution_count": null, |
| 227 | + "execution_count": 13, |
226 | 228 | "id": "4cb895db-d746-4af0-8e12-e7805865586d",
|
227 | 229 | "metadata": {},
|
228 | 230 | "outputs": [],
|
| 231 | + "source": [] |
| 232 | + }, |
| 233 | + { |
| 234 | + "cell_type": "code", |
| 235 | + "execution_count": 43, |
| 236 | + "id": "51a151c4-cf39-430d-bd49-c0f8fa0baab3", |
| 237 | + "metadata": {}, |
| 238 | + "outputs": [ |
| 239 | + { |
| 240 | + "name": "stdout", |
| 241 | + "output_type": "stream", |
| 242 | + "text": [ |
| 243 | + "run: micropython /home/jon/projects/emlearn-micropython/benchmarks/iir/iir_run.py /tmp/tmp3kr9y4bm/input.npy /tmp/tmp3kr9y4bm/output.npy /tmp/tmp3kr9y4bm/filter.npy\n", |
| 244 | + "b''\n" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "ename": "AssertionError", |
| 249 | + "evalue": "/tmp/tmp3kr9y4bm/output.npy", |
| 250 | + "output_type": "error", |
| 251 | + "traceback": [ |
| 252 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 253 | + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", |
| 254 | + "Cell \u001b[0;32mIn[43], line 32\u001b[0m\n\u001b[1;32m 30\u001b[0m noise \u001b[38;5;241m=\u001b[39m numpy\u001b[38;5;241m.\u001b[39mrandom\u001b[38;5;241m.\u001b[39mrandom(\u001b[38;5;241m1000\u001b[39m)\n\u001b[1;32m 31\u001b[0m out_scipy \u001b[38;5;241m=\u001b[39m scipy\u001b[38;5;241m.\u001b[39msignal\u001b[38;5;241m.\u001b[39msosfilt(coeff, noise)\n\u001b[0;32m---> 32\u001b[0m out_emlearn \u001b[38;5;241m=\u001b[39m \u001b[43msosfilt_emlearn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnumpy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcoeff\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnoise\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 34\u001b[0m fig, ax \u001b[38;5;241m=\u001b[39m plt\u001b[38;5;241m.\u001b[39msubplots(\u001b[38;5;241m1\u001b[39m, figsize\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m10\u001b[39m, \u001b[38;5;241m5\u001b[39m))\n\u001b[1;32m 35\u001b[0m plot_spectrum_difference(ax, noise, out_scipy, fs\u001b[38;5;241m=\u001b[39mfs, label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mscipy\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", |
| 255 | + "Cell \u001b[0;32mIn[43], line 24\u001b[0m, in \u001b[0;36msosfilt_emlearn\u001b[0;34m(coeff, input)\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 20\u001b[0m \u001b[38;5;124;03mCalls MicroPython in a subprocess, using eml_iir modules from emlearn-micropython\u001b[39;00m\n\u001b[1;32m 21\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 22\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01miir_run_subprocess\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m run_iir\n\u001b[0;32m---> 24\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43mrun_iir\u001b[49m\u001b[43m(\u001b[49m\u001b[43marray\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43marray\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43marray\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcoeff\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mflatten\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 26\u001b[0m out \u001b[38;5;241m=\u001b[39m numpy\u001b[38;5;241m.\u001b[39marray(out)\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m out\n", |
| 256 | + "File \u001b[0;32m~/projects/emlearn-micropython/benchmarks/iir/iir_run_subprocess.py:48\u001b[0m, in \u001b[0;36mrun_iir\u001b[0;34m(data, coefficients, micropython_bin)\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[38;5;28mprint\u001b[39m(out)\n\u001b[1;32m 47\u001b[0m \u001b[38;5;66;03m# load the output\u001b[39;00m\n\u001b[0;32m---> 48\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mexists(output_path), output_path\n\u001b[1;32m 49\u001b[0m output_shape, output \u001b[38;5;241m=\u001b[39m npyfile\u001b[38;5;241m.\u001b[39mload(output_path)\n\u001b[1;32m 51\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n", |
| 257 | + "\u001b[0;31mAssertionError\u001b[0m: /tmp/tmp3kr9y4bm/output.npy" |
| 258 | + ] |
| 259 | + } |
| 260 | + ], |
229 | 261 | "source": [
|
230 |
| - "# TODO: run tests with eml_iir and eml_iir_q15, using scipy sosfilt as reference" |
| 262 | + "\n", |
| 263 | + "def spectrum_welch(signal, sr, n_fft, window='hann'):\n", |
| 264 | + " freqs, power = scipy.signal.welch(signal, fs=sr, nperseg=n_fft, window=window, scaling=\"spectrum\", average='mean')\n", |
| 265 | + " db = librosa.power_to_db(power, ref=0.0, top_db=240)\n", |
| 266 | + " return pandas.Series(db, index=freqs)\n", |
| 267 | + "\n", |
| 268 | + "\n", |
| 269 | + "def plot_spectrum_difference(ax, input, output, fs, label=None, alpha=0.5, n_fft = 512):\n", |
| 270 | + " \n", |
| 271 | + " S_in = spectrum_welch(input, sr=fs, n_fft=n_fft)\n", |
| 272 | + " S_out = spectrum_welch(output, sr=fs, n_fft=n_fft)\n", |
| 273 | + " \n", |
| 274 | + " FR = S_out - S_in\n", |
| 275 | + " FR += numpy.random.random(1)\n", |
| 276 | + " ax.plot(FR.index, FR.values, label=label, alpha=alpha)\n", |
| 277 | + " ax.set_xscale('log')\n", |
| 278 | + " ax.set_xlim(1, fs)\n", |
| 279 | + "\n", |
| 280 | + "def sosfilt_emlearn(coeff : numpy.array, input : numpy.array):\n", |
| 281 | + " \"\"\"\n", |
| 282 | + " Calls MicroPython in a subprocess, using eml_iir modules from emlearn-micropython\n", |
| 283 | + " \"\"\"\n", |
| 284 | + " from iir_run_subprocess import run_iir\n", |
| 285 | + " \n", |
| 286 | + " out = run_iir(array.array('f', input), array.array('f', coeff.flatten()))\n", |
| 287 | + "\n", |
| 288 | + " out = numpy.array(out)\n", |
| 289 | + " \n", |
| 290 | + " return out\n", |
| 291 | + "\n", |
| 292 | + "noise = numpy.random.random(1000)\n", |
| 293 | + "out_scipy = scipy.signal.sosfilt(coeff, noise)\n", |
| 294 | + "out_emlearn = sosfilt_emlearn(numpy.array(coeff), noise)\n", |
| 295 | + "\n", |
| 296 | + "fig, ax = plt.subplots(1, figsize=(10, 5))\n", |
| 297 | + "plot_spectrum_difference(ax, noise, out_scipy, fs=fs, label='scipy')\n", |
| 298 | + "plot_spectrum_difference(ax, noise, out_emlearn, fs=fs, label='emlearn')\n", |
| 299 | + "ax.legend()\n", |
| 300 | + "\n", |
| 301 | + "#numpy.save('noise.npy', noise)\n" |
231 | 302 | ]
|
| 303 | + }, |
| 304 | + { |
| 305 | + "cell_type": "code", |
| 306 | + "execution_count": null, |
| 307 | + "id": "e1597230-78a9-45e2-a1af-e8bdc0e5388e", |
| 308 | + "metadata": {}, |
| 309 | + "outputs": [], |
| 310 | + "source": [] |
| 311 | + }, |
| 312 | + { |
| 313 | + "cell_type": "code", |
| 314 | + "execution_count": null, |
| 315 | + "id": "7546d0f2-29fa-420c-8b0a-13faf8919fc7", |
| 316 | + "metadata": {}, |
| 317 | + "outputs": [], |
| 318 | + "source": [] |
232 | 319 | }
|
233 | 320 | ],
|
234 | 321 | "metadata": {
|
|
0 commit comments