├── LICENSE
├── README.md
├── esp32-i2s-slm.ino
├── math
├── c_weighting.m
├── ics43432.m
├── ics43434.m
├── im69d130.m
├── inmp441.m
└── sph645lm4h-b.m
├── misc
├── esp32-i2s-slm-bp.svg
└── ics-43434-afr.svg
└── sos-iir-filter.h
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Sound Level Meter with Arduino IDE, ESP32 and I2S MEMS microphone
2 |
3 | Arduino/ESP32 Sound Level Meter (SLM) using inexpensive, factory calibrated, digital I2S MEMS microphone and digital IIR filters (vs. FFT) for equalization and A-weighting.
4 |
5 | The basic idea is:
6 |
7 | 
8 |
9 | And the microphone response after equalization should look like:
10 |
11 | 
12 |
13 | Theoretically, i.e. with factory calibrated ICS-4343x, this should get you ±1dB(A) measurement within 20Hz-20KHz range.
14 |
15 | The code in this repository is mostly intended as example how you can integrate resonable noise measurement (i.e. *L*Aeq, Equivalent Continuous Sound Level) in your projects.
16 |
17 | You can find a bit more information in my [hackday.io](https://hackaday.io/project/166867-esp32-i2s-slm) project.
18 |
--------------------------------------------------------------------------------
/esp32-i2s-slm.ino:
--------------------------------------------------------------------------------
1 | /*
2 | * Display A-weighted sound level measured by I2S Microphone
3 | *
4 | * (c)2019 Ivan Kostoski
5 | *
6 | * This program is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * This program is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with this program. If not, see .
18 | */
19 |
20 | /*
21 | * Sketch samples audio data from I2S microphone, processes the data
22 | * with digital IIR filters and calculates A or C weighted Equivalent
23 | * Continuous Sound Level (Leq)
24 | *
25 | * I2S is setup to sample data at Fs=48000KHz (fixed value due to
26 | * design of digital IIR filters). Data is read from I2S queue
27 | * in 'sample blocks' (default 125ms block, equal to 6000 samples)
28 | * by 'i2s_reader_task', filtered trough two IIR filters (equalizer
29 | * and weighting), summed up and pushed into 'samples_queue' as
30 | * sum of squares of filtered samples. The main task then pulls data
31 | * from the queue and calculates decibel value relative to microphone
32 | * reference amplitude, derived from datasheet sensitivity dBFS
33 | * value, number of bits in I2S data, and the reference value for
34 | * which the sensitivity is specified (typically 94dB, pure sine
35 | * wave at 1KHz).
36 | *
37 | * Displays line on the small OLED screen with 'short' LAeq(125ms)
38 | * response and numeric LAeq(1sec) dB value from the signal RMS.
39 | */
40 |
41 | #include
42 | #include "sos-iir-filter.h"
43 |
44 | //
45 | // Configuration
46 | //
47 |
48 | #define LEQ_PERIOD 1 // second(s)
49 | #define WEIGHTING C_weighting // Also avaliable: 'C_weighting' or 'None' (Z_weighting)
50 | #define LEQ_UNITS "LAeq" // customize based on above weighting used
51 | #define DB_UNITS "dBA" // customize based on above weighting used
52 | #define USE_DISPLAY 1
53 |
54 | // NOTE: Some microphones require at least DC-Blocker filter
55 | #define MIC_EQUALIZER ICS43434 // See below for defined IIR filters or set to 'None' to disable
56 | #define MIC_OFFSET_DB 3.0103 // Default offset (sine-wave RMS vs. dBFS). Modify this value for linear calibration
57 |
58 | // Customize these values from microphone datasheet
59 | #define MIC_SENSITIVITY -26 // dBFS value expected at MIC_REF_DB (Sensitivity value from datasheet)
60 | #define MIC_REF_DB 94.0 // Value at which point sensitivity is specified in datasheet (dB)
61 | #define MIC_OVERLOAD_DB 116.0 // dB - Acoustic overload point
62 | #define MIC_NOISE_DB 29 // dB - Noise floor
63 | #define MIC_BITS 24 // valid number of bits in I2S data
64 | #define MIC_CONVERT(s) (s >> (SAMPLE_BITS - MIC_BITS))
65 | #define MIC_TIMING_SHIFT 0 // Set to one to fix MSB timing for some microphones, i.e. SPH0645LM4H-x
66 |
67 | // Calculate reference amplitude value at compile time
68 | constexpr double MIC_REF_AMPL = pow(10, double(MIC_SENSITIVITY)/20) * ((1<<(MIC_BITS-1))-1);
69 |
70 | //
71 | // I2S pins - Can be routed to almost any (unused) ESP32 pin.
72 | // SD can be any pin, inlcuding input only pins (36-39).
73 | // SCK (i.e. BCLK) and WS (i.e. L/R CLK) must be output capable pins
74 | //
75 | // Below ones are just example for my board layout, put here the pins you will use
76 | //
77 | #define I2S_WS 18
78 | #define I2S_SCK 23
79 | #define I2S_SD 19
80 |
81 | // I2S peripheral to use (0 or 1)
82 | #define I2S_PORT I2S_NUM_0
83 |
84 | //
85 | // Setup your display library (and geometry) here
86 | //
87 | #if (USE_DISPLAY > 0)
88 | // ThingPulse/esp8266-oled-ssd1306, you may need the latest source and PR#198 for 64x48
89 | #include
90 | #define OLED_GEOMETRY GEOMETRY_64_48
91 | //#define OLED_GEOMETRY GEOMETRY_128_32
92 | //#define OLED_GEOMETRY GEOMETRY_128_64
93 | #define OLED_FLIP_V 1
94 | SSD1306Wire display(0x3c, SDA, SCL, OLED_GEOMETRY);
95 | #endif
96 |
97 |
98 | //
99 | // IIR Filters
100 | //
101 |
102 | // DC-Blocker filter - removes DC component from I2S data
103 | // See: https://www.dsprelated.com/freebooks/filters/DC_Blocker.html
104 | // a1 = -0.9992 should heavily attenuate frequencies below 10Hz
105 | SOS_IIR_Filter DC_BLOCKER = {
106 | gain: 1.0,
107 | sos: {{-1.0, 0.0, +0.9992, 0}}
108 | };
109 |
110 | //
111 | // Equalizer IIR filters to flatten microphone frequency response
112 | // See respective .m file for filter design. Fs = 48Khz.
113 | //
114 | // Filters are represented as Second-Order Sections cascade with assumption
115 | // that b0 and a0 are equal to 1.0 and 'gain' is applied at the last step
116 | // B and A coefficients were transformed with GNU Octave:
117 | // [sos, gain] = tf2sos(B, A)
118 | // See: https://www.dsprelated.com/freebooks/filters/Series_Second_Order_Sections.html
119 | // NOTE: SOS matrix 'a1' and 'a2' coefficients are negatives of tf2sos output
120 | //
121 |
122 | // TDK/InvenSense ICS-43434
123 | // Datasheet: https://www.invensense.com/wp-content/uploads/2016/02/DS-000069-ICS-43434-v1.1.pdf
124 | // B = [0.477326418836803, -0.486486982406126, -0.336455844522277, 0.234624646917202, 0.111023257388606];
125 | // A = [1.0, -1.93073383849136326, 0.86519456089576796, 0.06442838283825100, 0.00111249298800616];
126 | SOS_IIR_Filter ICS43434 = {
127 | gain: 0.477326418836803,
128 | sos: { // Second-Order Sections {b1, b2, -a1, -a2}
129 | {+0.96986791463971267, 0.23515976355743193, -0.06681948004769928, -0.00111521990688128},
130 | {-1.98905931743624453, 0.98908924206960169, +1.99755331853906037, -0.99755481510122113}
131 | }
132 | };
133 |
134 | // TDK/InvenSense ICS-43432
135 | // Datasheet: https://www.invensense.com/wp-content/uploads/2015/02/ICS-43432-data-sheet-v1.3.pdf
136 | // B = [-0.45733702338341309 1.12228667105574775 -0.77818278904413563, 0.00968926337978037, 0.10345668405223755]
137 | // A = [1.0, -3.3420781082912949, 4.4033694320978771, -3.0167072679918010, 1.2265536567647031, -0.2962229189311990, 0.0251085747458112]
138 | SOS_IIR_Filter ICS43432 = {
139 | gain: -0.457337023383413,
140 | sos: { // Second-Order Sections {b1, b2, -a1, -a2}
141 | {-0.544047931916859, -0.248361759321800, +0.403298891662298, -0.207346186351843},
142 | {-1.909911869441421, +0.910830292683527, +1.790285722826743, -0.804085812369134},
143 | {+0.000000000000000, +0.000000000000000, +1.148493493802252, -0.150599527756651}
144 | }
145 | };
146 |
147 | // TDK/InvenSense INMP441
148 | // Datasheet: https://www.invensense.com/wp-content/uploads/2015/02/INMP441.pdf
149 | // B ~= [1.00198, -1.99085, 0.98892]
150 | // A ~= [1.0, -1.99518, 0.99518]
151 | SOS_IIR_Filter INMP441 = {
152 | gain: 1.00197834654696,
153 | sos: { // Second-Order Sections {b1, b2, -a1, -a2}
154 | {-1.986920458344451, +0.986963226946616, +1.995178510504166, -0.995184322194091}
155 | }
156 | };
157 |
158 | // Infineon IM69D130 Shield2Go
159 | // Datasheet: https://www.infineon.com/dgdl/Infineon-IM69D130-DS-v01_00-EN.pdf?fileId=5546d462602a9dc801607a0e46511a2e
160 | // B ~= [1.001240684967527, -1.996936108836337, 0.995703101823006]
161 | // A ~= [1.0, -1.997675693595542, 0.997677044195563]
162 | // With additional DC blocking component
163 | SOS_IIR_Filter IM69D130 = {
164 | gain: 1.00124068496753,
165 | sos: {
166 | {-1.0, 0.0, +0.9992, 0}, // DC blocker, a1 = -0.9992
167 | {-1.994461610298131, 0.994469278738208, +1.997675693595542, -0.997677044195563}
168 | }
169 | };
170 |
171 | // Knowles SPH0645LM4H-B, rev. B
172 | // https://cdn-shop.adafruit.com/product-files/3421/i2S+Datasheet.PDF
173 | // B ~= [1.001234, -1.991352, 0.990149]
174 | // A ~= [1.0, -1.993853, 0.993863]
175 | // With additional DC blocking component
176 | SOS_IIR_Filter SPH0645LM4H_B_RB = {
177 | gain: 1.00123377961525,
178 | sos: { // Second-Order Sections {b1, b2, -a1, -a2}
179 | {-1.0, 0.0, +0.9992, 0}, // DC blocker, a1 = -0.9992
180 | {-1.988897663539382, +0.988928479008099, +1.993853376183491, -0.993862821429572}
181 | }
182 | };
183 |
184 | //
185 | // Weighting filters
186 | //
187 |
188 | //
189 | // A-weighting IIR Filter, Fs = 48KHz
190 | // (By Dr. Matt L., Source: https://dsp.stackexchange.com/a/36122)
191 | // B = [0.169994948147430, 0.280415310498794, -1.120574766348363, 0.131562559965936, 0.974153561246036, -0.282740857326553, -0.152810756202003]
192 | // A = [1.0, -2.12979364760736134, 0.42996125885751674, 1.62132698199721426, -0.96669962900852902, 0.00121015844426781, 0.04400300696788968]
193 | SOS_IIR_Filter A_weighting = {
194 | gain: 0.169994948147430,
195 | sos: { // Second-Order Sections {b1, b2, -a1, -a2}
196 | {-2.00026996133106, +1.00027056142719, -1.060868438509278, -0.163987445885926},
197 | {+4.35912384203144, +3.09120265783884, +1.208419926363593, -0.273166998428332},
198 | {-0.70930303489759, -0.29071868393580, +1.982242159753048, -0.982298594928989}
199 | }
200 | };
201 |
202 | //
203 | // C-weighting IIR Filter, Fs = 48KHz
204 | // Designed by invfreqz curve-fitting, see respective .m file
205 | // B = [-0.49164716933714026, 0.14844753846498662, 0.74117815661529129, -0.03281878334039314, -0.29709276192593875, -0.06442545322197900, -0.00364152725482682]
206 | // A = [1.0, -1.0325358998928318, -0.9524000181023488, 0.8936404694728326 0.2256286147169398 -0.1499917107550188, 0.0156718181681081]
207 | SOS_IIR_Filter C_weighting = {
208 | gain: -0.491647169337140,
209 | sos: {
210 | {+1.4604385758204708, +0.5275070373815286, +1.9946144559930252, -0.9946217070140883},
211 | {+0.2376222404939509, +0.0140411206016894, -1.3396585608422749, -0.4421457807694559},
212 | {-2.0000000000000000, +1.0000000000000000, +0.3775800047420818, -0.0356365756680430}
213 | }
214 | };
215 |
216 |
217 | //
218 | // Sampling
219 | //
220 | #define SAMPLE_RATE 48000 // Hz, fixed to design of IIR filters
221 | #define SAMPLE_BITS 32 // bits
222 | #define SAMPLE_T int32_t
223 | #define SAMPLES_SHORT (SAMPLE_RATE / 8) // ~125ms
224 | #define SAMPLES_LEQ (SAMPLE_RATE * LEQ_PERIOD)
225 | #define DMA_BANK_SIZE (SAMPLES_SHORT / 16)
226 | #define DMA_BANKS 32
227 |
228 | // Data we push to 'samples_queue'
229 | struct sum_queue_t {
230 | // Sum of squares of mic samples, after Equalizer filter
231 | float sum_sqr_SPL;
232 | // Sum of squares of weighted mic samples
233 | float sum_sqr_weighted;
234 | // Debug only, FreeRTOS ticks we spent processing the I2S data
235 | uint32_t proc_ticks;
236 | };
237 | QueueHandle_t samples_queue;
238 |
239 | // Static buffer for block of samples
240 | float samples[SAMPLES_SHORT] __attribute__((aligned(4)));
241 |
242 | //
243 | // I2S Microphone sampling setup
244 | //
245 | void mic_i2s_init() {
246 | // Setup I2S to sample mono channel for SAMPLE_RATE * SAMPLE_BITS
247 | // NOTE: Recent update to Arduino_esp32 (1.0.2 -> 1.0.3)
248 | // seems to have swapped ONLY_LEFT and ONLY_RIGHT channels
249 | const i2s_config_t i2s_config = {
250 | mode: i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
251 | sample_rate: SAMPLE_RATE,
252 | bits_per_sample: i2s_bits_per_sample_t(SAMPLE_BITS),
253 | channel_format: I2S_CHANNEL_FMT_ONLY_LEFT,
254 | communication_format: i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
255 | intr_alloc_flags: ESP_INTR_FLAG_LEVEL1,
256 | dma_buf_count: DMA_BANKS,
257 | dma_buf_len: DMA_BANK_SIZE,
258 | use_apll: true,
259 | tx_desc_auto_clear: false,
260 | fixed_mclk: 0
261 | };
262 | // I2S pin mapping
263 | const i2s_pin_config_t pin_config = {
264 | bck_io_num: I2S_SCK,
265 | ws_io_num: I2S_WS,
266 | data_out_num: -1, // not used
267 | data_in_num: I2S_SD
268 | };
269 |
270 | i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
271 |
272 | #if (MIC_TIMING_SHIFT > 0)
273 | // Undocumented (?!) manipulation of I2S peripheral registers
274 | // to fix MSB timing issues with some I2S microphones
275 | REG_SET_BIT(I2S_TIMING_REG(I2S_PORT), BIT(9));
276 | REG_SET_BIT(I2S_CONF_REG(I2S_PORT), I2S_RX_MSB_SHIFT);
277 | #endif
278 |
279 | i2s_set_pin(I2S_PORT, &pin_config);
280 |
281 | //FIXME: There is a known issue with esp-idf and sampling rates, see:
282 | // https://github.com/espressif/esp-idf/issues/2634
283 | // In the meantime, the below line seems to set sampling rate at ~47999.992Hz
284 | // fifs_req=24576000, sdm0=149, sdm1=212, sdm2=5, odir=2 -> fifs_reached=24575996
285 | //NOTE: This seems to be fixed in ESP32 Arduino 1.0.4, esp-idf 3.2
286 | // Should be safe to remove...
287 | //#include
288 | //rtc_clk_apll_enable(1, 149, 212, 5, 2);
289 | }
290 |
291 | //
292 | // I2S Reader Task
293 | //
294 | // Rationale for separate task reading I2S is that IIR filter
295 | // processing cam be scheduled to different core on the ESP32
296 | // while main task can do something else, like update the
297 | // display in the example
298 | //
299 | // As this is intended to run as separate hihg-priority task,
300 | // we only do the minimum required work with the I2S data
301 | // until it is 'compressed' into sum of squares
302 | //
303 | // FreeRTOS priority and stack size (in 32-bit words)
304 | #define I2S_TASK_PRI 4
305 | #define I2S_TASK_STACK 2048
306 | //
307 | void mic_i2s_reader_task(void* parameter) {
308 | mic_i2s_init();
309 |
310 | // Discard first block, microphone may have startup time (i.e. INMP441 up to 83ms)
311 | size_t bytes_read = 0;
312 | i2s_read(I2S_PORT, &samples, SAMPLES_SHORT * sizeof(int32_t), &bytes_read, portMAX_DELAY);
313 |
314 | while (true) {
315 | // Block and wait for microphone values from I2S
316 | //
317 | // Data is moved from DMA buffers to our 'samples' buffer by the driver ISR
318 | // and when there is requested ammount of data, task is unblocked
319 | //
320 | // Note: i2s_read does not care it is writing in float[] buffer, it will write
321 | // integer values to the given address, as received from the hardware peripheral.
322 | i2s_read(I2S_PORT, &samples, SAMPLES_SHORT * sizeof(SAMPLE_T), &bytes_read, portMAX_DELAY);
323 |
324 | TickType_t start_tick = xTaskGetTickCount();
325 |
326 | // Convert (including shifting) integer microphone values to floats,
327 | // using the same buffer (assumed sample size is same as size of float),
328 | // to save a bit of memory
329 | SAMPLE_T* int_samples = (SAMPLE_T*)&samples;
330 | for(int i=0; i 0)
365 | display.init();
366 | #if (OLED_FLIP_V > 0)
367 | display.flipScreenVertically();
368 | #endif
369 | display.setFont(ArialMT_Plain_16);
370 | #endif
371 |
372 | // Create FreeRTOS queue
373 | samples_queue = xQueueCreate(8, sizeof(sum_queue_t));
374 |
375 | // Create the I2S reader FreeRTOS task
376 | // NOTE: Current version of ESP-IDF will pin the task
377 | // automatically to the first core it happens to run on
378 | // (due to using the hardware FPU instructions).
379 | // For manual control see: xTaskCreatePinnedToCore
380 | xTaskCreate(mic_i2s_reader_task, "Mic I2S Reader", I2S_TASK_STACK, NULL, I2S_TASK_PRI, NULL);
381 |
382 | sum_queue_t q;
383 | uint32_t Leq_samples = 0;
384 | double Leq_sum_sqr = 0;
385 | double Leq_dB = 0;
386 |
387 | // Read sum of samaples, calculated by 'i2s_reader_task'
388 | while (xQueueReceive(samples_queue, &q, portMAX_DELAY)) {
389 |
390 | // Calculate dB values relative to MIC_REF_AMPL and adjust for microphone reference
391 | double short_RMS = sqrt(double(q.sum_sqr_SPL) / SAMPLES_SHORT);
392 | double short_SPL_dB = MIC_OFFSET_DB + MIC_REF_DB + 20 * log10(short_RMS / MIC_REF_AMPL);
393 |
394 | // In case of acoustic overload or below noise floor measurement, report infinty Leq value
395 | if (short_SPL_dB > MIC_OVERLOAD_DB) {
396 | Leq_sum_sqr = INFINITY;
397 | } else if (isnan(short_SPL_dB) || (short_SPL_dB < MIC_NOISE_DB)) {
398 | Leq_sum_sqr = -INFINITY;
399 | }
400 |
401 | // Accumulate Leq sum
402 | Leq_sum_sqr += q.sum_sqr_weighted;
403 | Leq_samples += SAMPLES_SHORT;
404 |
405 | // When we gather enough samples, calculate new Leq value
406 | if (Leq_samples >= SAMPLE_RATE * LEQ_PERIOD) {
407 | double Leq_RMS = sqrt(Leq_sum_sqr / Leq_samples);
408 | Leq_dB = MIC_OFFSET_DB + MIC_REF_DB + 20 * log10(Leq_RMS / MIC_REF_AMPL);
409 | Leq_sum_sqr = 0;
410 | Leq_samples = 0;
411 |
412 | // Serial output, customize (or remove) as needed
413 | Serial.printf("%.1f\n", Leq_dB);
414 |
415 | // Debug only
416 | //Serial.printf("%u processing ticks\n", q.proc_ticks);
417 | }
418 |
419 | #if (USE_DISPLAY > 0)
420 |
421 | //
422 | // Example code that displays the measured value.
423 | // You should customize the below code for your display
424 | // and display library used.
425 | //
426 |
427 | display.clear();
428 |
429 | // It is important to somehow notify when the deivce is out of its range
430 | // as the calculated values are very likely with big error
431 | if (Leq_dB > MIC_OVERLOAD_DB) {
432 | // Display 'Overload' if dB value is over the AOP
433 | display.drawString(0, 24, "Overload");
434 | } else if (isnan(Leq_dB) || (Leq_dB < MIC_NOISE_DB)) {
435 | // Display 'Low' if dB value is below noise floor
436 | display.drawString(0, 24, "Low");
437 | }
438 |
439 | // The 'short' Leq line
440 | double short_Leq_dB = MIC_OFFSET_DB + MIC_REF_DB + 20 * log10(sqrt(double(q.sum_sqr_weighted) / SAMPLES_SHORT) / MIC_REF_AMPL);
441 | uint16_t len = min(max(0, int(((short_Leq_dB - MIC_NOISE_DB) / MIC_OVERLOAD_DB) * (display.getWidth()-1))), display.getWidth()-1);
442 | display.drawHorizontalLine(0, 0, len);
443 | display.drawHorizontalLine(0, 1, len);
444 | display.drawHorizontalLine(0, 2, len);
445 |
446 | // The Leq numeric decibels
447 | display.drawString(0, 4, String(Leq_dB, 1) + " " + DB_UNITS);
448 |
449 | display.display();
450 |
451 | #endif // USE_DISPLAY
452 | }
453 | }
454 |
455 | void loop() {
456 | // Nothing here..
457 | }
458 |
--------------------------------------------------------------------------------
/math/c_weighting.m:
--------------------------------------------------------------------------------
1 | %
2 | % C-weighting IIR filter design
3 | %
4 | % (c)2019 Ivan Kostoski
5 | %
6 |
7 | clear;
8 | format long;
9 | pi = 3.14159265358979;
10 |
11 | % Sampling Rate
12 | Fs = 48000;
13 |
14 | % IEC specified frequencies
15 | IEC_f = [10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, ...
16 | 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, ...
17 | 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, ...
18 | 10000, 12500, 16000, 20000];
19 |
20 | % Logarithmic frequencies for graphs
21 | f = logspace(0, 5, 2048);
22 |
23 | % IEC specified attenuation for C-weighting
24 | IEC_f_cw = [-14.3, -11.2, -8.5, -6.2, -4.4, -3.0, -2.0, -1.3, -.8, -.5, ...
25 | -.3, -.2, -.1, 0, 0, 0, 0, 0, 0, 0, ...
26 | 0, 0, -.1, -.2, -.3, -.5, -.8, -1.3, -2.0, -3.0, ...
27 | -4.4, -6.2, -8.5, -11.2];
28 |
29 | % Analog C-weighting filter according to IEC/CD 1672.
30 | f1 = 20.598997; %Hz
31 | f2 = 107.65265; %Hz
32 | f3 = 737.86223; %Hz
33 | f4 = 12194.217; %Hz
34 | C1000 = 0.0619;
35 | NUM = [(2*pi*f4)^2*(10^(C1000/20)), 0, 0];
36 | DEN = conv([1, +4*pi*f4, (2*pi*f4)^2], [1, +4*pi*f1, (2*pi*f1)^2]);
37 | % Analog transfer function
38 | H_a = freqs(NUM, DEN, f.*(2*pi));
39 |
40 | % Bilinear transformation of analog design to get the digital
41 | [B_bt, A_bt] = bilinear(NUM, DEN, 1/Fs);
42 | H_bt = freqz(B_bt, A_bt, f, Fs);
43 |
44 | % We will take the only the low frequency part of bilinear transformation
45 | % Which was calculated to be approximately (the first SOS):
46 | B_lo = [1, -2, 1];
47 | A_lo = [1, -1.9946144559930206, 0.9946217070140836];
48 | [sos_lo, gain_lo] = tf2sos(B_lo, A_lo);
49 |
50 | %
51 | % Curve fitting of the 'high' frequency part with invfreqz
52 | %
53 | f_hi = [1000, 1250, 10000, 16000, 20000, 24000];
54 | dB_hi = [ 0, 0, -4.4, -8.6, -11.3, -15.0];
55 | w_hi = f_hi.*((2*pi)/Fs);
56 | mag_hi = arrayfun(@db2mag, dB_hi);
57 | [B_hi, A_hi] = invfreqz(mag_hi, w_hi, 4, 4);
58 | [sos_hi, gain_hi] = tf2sos(B_hi, A_hi);
59 |
60 | % Merge SOS sections
61 | sos = [sos_lo; sos_hi]
62 | % Multiply the gain and reduce it a bit
63 | gain = gain_lo * gain_hi * 0.996
64 |
65 | % Convert to transfer function and try to stabilize
66 | [B_tf, A_tf] = sos2tf(sos, gain);
67 | B_tf_s = polystab(B_tf) * norm(B_tf) / norm(polystab(B_tf));
68 | A_tf_s = polystab(A_tf) * norm(A_tf) / norm(polystab(A_tf));
69 | A_tf_s0 = A_tf_s(1);
70 | % Caluclate and display stable coefficients
71 | B = B_tf_s./A_tf_s0
72 | A = A_tf_s./A_tf_s0
73 | % Display the roots
74 | roots(B)
75 | roots(A)
76 | H = freqz(B, A, f, Fs);
77 |
78 | % Frequency response graph
79 | clf;
80 | hold on;
81 | grid on;
82 | xlim([10, Fs/2]);
83 | ylim([-15, 5]);
84 | xlabel('Frequency (Hz)');
85 | ylabel('Magnitude (dB)');
86 | semilogx(f, 20*log10(abs(H_a)), 'g;IEC specified transfer function for C-weighting;');
87 | semilogx(f, 20*log10(abs(H_bt)), 'r;Bilinear trasformation;');
88 | semilogx(f, 20*log10(abs(H)), 'b;High frequency part curve fitting with invfreqz;');
89 | hold off
90 |
91 | % Filter error graph
92 | clf;
93 | hold on;
94 | grid on;
95 | grid minor;
96 | xlim([10, 20000]);
97 | ylim([-1, +1]);
98 | xlabel('Frequency (Hz)');
99 | ylabel('Filter error (dB)');
100 | semilogx(f, 20*log10(abs(H_bt)) - 20*log10(abs(H_a)), 'r;Error with just bilinear trasformation filter;');
101 | semilogx(f, 20*log10(abs(H)) - 20*log10(abs(H_a)), 'b;Error with additional curve fitting via invfreqz;');
102 | hold off;
103 |
104 | % Display the sections
105 | [sos1, gain1] = tf2sos(B, A)
106 |
--------------------------------------------------------------------------------
/math/ics43432.m:
--------------------------------------------------------------------------------
1 | %
2 | % ICS-43432 Equalizer filter design
3 | %
4 | % (c)2019 Ivan Kostoski
5 | %
6 |
7 | clear;
8 | Fs = 48000;
9 | format long G;
10 |
11 | % IEC specified frequencies
12 | iec_f = [10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, ...
13 | 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, ...
14 | 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, ...
15 | 10000, 12500, 16000, 20000];
16 |
17 | % Values visually estimated from ICS-43432 datasheet for IEC frequencies
18 | % https://www.invensense.com/wp-content/uploads/2015/02/ICS-43432-data-sheet-v1.3.pdf
19 | % Figure 5 on page 9
20 | % 10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80,
21 | ds_e_dB = [ -22, -19, -15, -12, -8, -6, -4.5, -3.5, -2.1, -1.1, ...
22 | -0.4, +0.1, +0.6, +0.9, +1.0, 0.9, +0.8, +0.6, +0.4, +0.3, ...
23 | 0, +0.3, +0.4, +0.5, +0.7, +1.0, +1.2, +1.4, +1.8, +2.3, ...
24 | +3.5, +6.0, +9.0, +14.0];
25 |
26 | % Manually chosen and adjusted values for fitting the transfer function
27 | ds_f = [ 20, 100, 630, 1150, 2000, 10000, 20000];
28 | ds_dB = [-16, -0.5, +0.7, 0, +0.5, +3.5, +14];
29 | [ds_B, ds_A] = invfreqz(arrayfun(@db2mag, ds_dB), (ds_f.*((2*pi)/Fs)), 6, 4);
30 | % Stabilize polynoms
31 | ds_Bs = polystab(ds_B) * norm(ds_B) / norm(polystab(ds_B));
32 | ds_As = polystab(ds_A) * norm(ds_A) / norm(polystab(ds_A));
33 | ds_H = freqz(ds_Bs, ds_As, iec_f, Fs);
34 |
35 | % Equalizer filter, i.e. inverse from estimated transfer filter
36 | % Swap A and B coefficients, and normalize to ds_B(1)
37 | eq_B = ds_As./ds_Bs(1)
38 | eq_A = ds_Bs./ds_Bs(1)
39 | eq_H = freqz(eq_B, eq_A, iec_f, Fs);
40 | % Check for poles ouside unit circle
41 | roots(eq_B)
42 | roots(eq_A)
43 |
44 | clf;
45 | hold on;
46 | semilogx(iec_f, ds_e_dB, 'g;ICS-43432 Datasheet plot (estimated);');
47 | semilogx(iec_f, 20 * log10 (abs (ds_H)), 'r;IIR filter frequency response;');
48 | semilogx(iec_f, (ds_e_dB + 20*log10(abs(eq_H))), 'b;Adjusted frequency response;', 'linewidth', 3);
49 | title("ICS-43432 Adjusted frequency response");
50 | grid minor;
51 | xlabel('Frequency (Hz)');
52 | xlim([10, 20000]);
53 | ylabel('Amplitude (dB)');
54 | ylim([-20, 20]);
55 | legend ('boxoff');
56 | legend ('location', 'northwest');
57 | ylim ([-20, 20]);
58 | grid on;
59 | hold off;
60 |
61 | [sos, gain] = tf2sos(eq_B, eq_A)
62 |
--------------------------------------------------------------------------------
/math/ics43434.m:
--------------------------------------------------------------------------------
1 | %
2 | % ICS-43434 Equalizer filter design
3 | %
4 | % (c)2019 Ivan Kostoski
5 | %
6 |
7 | clear;
8 | % Sampling frequency
9 | Fs = 48000;
10 |
11 | % IEC specified frequencies
12 | iec_f = [10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, ...
13 | 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, ...
14 | 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, ...
15 | 10000, 12500, 16000, 20000];
16 |
17 | % IEC Class 1 tolerances (top/bottom)
18 | iec_c1_t_dB = [+3.5, +3.0, +2.5, +2.5, +2.5, +2.0, +1.5, +1.5, +1.5, +1.5, ...
19 | +1.5, +1.5, +1.5, +1.5, +1.4, +1.4, +1.4, +1.4, +1.4, +1.4, ...
20 | +1.1, +1.4, +1.4, +1.6, +1.6, +1.6, +1.6, +2.1, +2.1, +2.1, ...
21 | +2.6, +3.0, +3.5, +4.0];
22 | iec_c1_b_dB = [-inf, -inf, -4.5, -2.5, -2.5, -2.0, -1.5, -1.5, -1.5, -1.5, ...
23 | -1.5, -1.5, -1.5, -1.5, -1.4, -1.4, -1.4, -1.4, -1.4, -1.4, ...
24 | -1.1, -1.4, -1.4, -1.6, -1.6, -1.6, -1.6, -2.1, -2.6, -3.1, ...
25 | -3.6, -6.0, -17, -inf];
26 |
27 | % IEC Class 2 tolerances (top/bottom)
28 | iec_c2_t_dB = [+5.5, +5.5, +5.5, +3.5, +3.5, +3.5, +2.5, +2.5, +2.5, +2.5, ...
29 | +2.0, +2.0, +2.0, +2.0, +1.9, +1.9, +1.9, +1.9, +1.9, +1.9, ...
30 | +1.4, +1.9, +2.6, +2.6, +3.1, +3.1, +3.6, +4.1, +5.1, +5.6, ...
31 | +6.0, +6.0, +6.0, +6.0];
32 | iec_c2_b_dB = [-inf, -inf, -inf, -3.5, -3.5, -3.5, -2.5, -2.5, -2.5, -2.5, ...
33 | -2.0, -2.0, -2.0, -2.0, -1.9, -1.9, -1.9, -1.9, -1.9, -1.9, ...
34 | -1.4, -1.9, -2.6, -2.6, -3.1, -3.1, -3.6, -4.1, -5.1, -5.6, ...
35 | -inf, -inf, -inf, -inf];
36 |
37 | % Values visually estimated from ICS-43434 datasheet 'Typical Frequency Response' plot
38 | ds_dB = [ -26, -22, -17, -13, -10, -8, -6, -4, -2.8, -2.1, ...
39 | -1.8, -1.4, -1.0, -0.5, -0.5, 0, 0, 0, 0, 0, ...
40 | 0, 0, 0, 0, 0, +0.5, +0.6, +1.1, +1.8, +2.5, ...
41 | +3.1, +4.5, +8.0, +14.0];
42 |
43 | % These value are selected and adjusted for better curve fit
44 | ds_l_f = [ 10, 20, 50, 100, 1000];
45 | ds_l_dB = [-26, -12, -3.5, -1.5, 0];
46 | ds_h_f = [ 1000, 5000, 10000, 20000];
47 | ds_h_dB = [ 0, +1.1, +3.1, +14.0];
48 |
49 | % Low frequency filter design
50 | % Convert Hz in rad/s and normalize for Fs
51 | ds_l_w = ds_l_f.*((2*pi)/Fs);
52 | % Convert plot decibels to magnitude
53 | ds_l_mag = arrayfun(@db2mag, ds_l_dB);
54 | % Estimate coefficients
55 | [ds_l_B, ds_l_A] = invfreqz(ds_l_mag, ds_l_w, 2, 2);
56 | % Stabilize and normalize the filter
57 | ds_l_As = polystab(ds_l_A) * norm(ds_l_A) / norm(polystab(ds_l_A));
58 | ds_l_Bs = polystab(ds_l_B) * norm(ds_l_B) / norm(polystab(ds_l_B));
59 |
60 | % High frequency filter design
61 | [ds_h_B, ds_h_A] = invfreqz(arrayfun(@db2mag, ds_h_dB), (ds_h_f.*((2*pi)/Fs)) , 2, 2);
62 | ds_h_As = polystab(ds_h_A) * norm(ds_h_A) / norm(polystab(ds_h_A));
63 | ds_h_Bs = polystab(ds_h_B) * norm(ds_h_B) / norm(polystab(ds_h_B));
64 |
65 | % Convolve into single 4th order filter
66 | ds_A = conv(ds_l_As, ds_h_As);
67 | ds_B = conv(ds_l_Bs, ds_h_Bs);
68 | ds_H = freqz(ds_B, ds_A, iec_f, Fs);
69 |
70 | % Equalizer filter, i.e. inverse from estimated transfer filter
71 | % Swap A and B coefficients, and normalize to ds_B(1)
72 | eq_B = ds_A./ds_B(1)
73 | eq_A = ds_B./ds_B(1)
74 | eq_H = freqz(eq_B, eq_A, iec_f, Fs);
75 | % Check for poles ouside unit circle
76 | roots(eq_B)
77 | roots(eq_A)
78 |
79 | clf;
80 | semilogx(iec_f, ds_dB, 'g;ICS-43434 Datasheet plot (approx.);');
81 | hold on;
82 | title("ICS-43434 Frequency response");
83 | grid minor;
84 | xlabel('Frequency (Hz)');
85 | xlim([10, 20000]);
86 | ylabel('Amplitude (dB)');
87 | ylim([-20, 20]);
88 | legend ('boxoff');
89 | legend ('location', 'northwest');
90 | semilogx(iec_f, iec_c1_t_dB, '--r;IEC 61672-1:2013 Class 1 tolerance;');
91 | semilogx(iec_f, iec_c1_b_dB, '--r');
92 | semilogx(iec_f, iec_c2_t_dB, 'r;IEC 61672-1:2013 Class 2 tolerance;');
93 | semilogx(iec_f, iec_c2_b_dB, 'r');
94 | semilogx(iec_f, 20*log10(abs(ds_H)), '--c;IIR filter frequency response;');
95 | semilogx(iec_f, (ds_dB + 20*log10(abs(eq_H))), 'b;Adjusted frequency response;', 'linewidth', 3);
96 | hold off;
97 |
--------------------------------------------------------------------------------
/math/im69d130.m:
--------------------------------------------------------------------------------
1 | %
2 | % IM69D130 Equalizer filter design
3 | %
4 | % (c)2019 Ivan Kostoski
5 | %
6 |
7 | clear;
8 | clf;
9 | format long;
10 | pi = 3.14159265358979;
11 |
12 | % Sampling Rate
13 | Fs = 48000;
14 |
15 | % IEC specified frequencies
16 | iec_f = [10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, ...
17 | 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, ...
18 | 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, ...
19 | 10000, 12500, 16000, 20000];
20 |
21 | % IEC Class 1 tolerances (top/bottom)
22 | iec_c1_t_dB = [+3.5, +3.0, +2.5, +2.5, +2.5, +2.0, +1.5, +1.5, +1.5, +1.5, ...
23 | +1.5, +1.5, +1.5, +1.5, +1.4, +1.4, +1.4, +1.4, +1.4, +1.4, ...
24 | +1.1, +1.4, +1.4, +1.6, +1.6, +1.6, +1.6, +2.1, +2.1, +2.1, ...
25 | +2.6, +3.0, +3.5, +4.0];
26 | iec_c1_b_dB = [-inf, -inf, -4.5, -2.5, -2.5, -2.0, -1.5, -1.5, -1.5, -1.5, ...
27 | -1.5, -1.5, -1.5, -1.5, -1.4, -1.4, -1.4, -1.4, -1.4, -1.4, ...
28 | -1.1, -1.4, -1.4, -1.6, -1.6, -1.6, -1.6, -2.1, -2.6, -3.1, ...
29 | -3.6, -6.0, -17, -inf];
30 |
31 | % IEC Class 2 tolerances (top/bottom)
32 | iec_c2_t_dB = [+5.5, +5.5, +5.5, +3.5, +3.5, +3.5, +2.5, +2.5, +2.5, +2.5, ...
33 | +2.0, +2.0, +2.0, +2.0, +1.9, +1.9, +1.9, +1.9, +1.9, +1.9, ...
34 | +1.4, +1.9, +2.6, +2.6, +3.1, +3.1, +3.6, +4.1, +5.1, +5.6, ...
35 | +6.0, +6.0, +6.0, +6.0];
36 | iec_c2_b_dB = [-inf, -inf, -inf, -3.5, -3.5, -3.5, -2.5, -2.5, -2.5, -2.5, ...
37 | -2.0, -2.0, -2.0, -2.0, -1.9, -1.9, -1.9, -1.9, -1.9, -1.9, ...
38 | -1.4, -1.9, -2.6, -2.6, -3.1, -3.1, -3.6, -4.1, -5.1, -5.6, ...
39 | -inf, -inf, -inf, -inf];
40 |
41 | % Values visually estimated from datasheet
42 | ds_dB = [ -inf, -inf, -inf, -5, -3.2, -2.5, -1.8, -1.1, -0.8, -0.6, ...
43 | -0.4, -0.2, -0.1, 0, 0, 0, 0, 0, 0, 0, ...
44 | 0, 0, 0, 0, 0, 0, 0, 0, -0.1, -0.2, ...
45 | -0.3, -0.4, +0.8, +2.5];
46 |
47 | % These value are selected and adjusted for better curve fit
48 | ds_l_w = [ 20, 31.5, 50, 1000];
49 | ds_l_dB = [ -4, -3.2, -1.1, 0];
50 |
51 | % Low frequency filter design
52 | % Convert Hz in rad/s and normalize for Fs
53 | ds_l_wn = ds_l_w.*((2*pi)/Fs);
54 | % Convert plot decibels to magnitude
55 | ds_l_mag = arrayfun(@db2mag, ds_l_dB);
56 | % Estimate coefficients
57 | [ds_l_B, ds_l_A] = invfreqz(ds_l_mag, ds_l_wn, 2, 2);
58 | % Stabilize and normalize the filter
59 | ds_B = polystab(ds_l_B) * norm(ds_l_B) / norm(polystab(ds_l_B));
60 | ds_A = polystab(ds_l_A) * norm(ds_l_A) / norm(polystab(ds_l_A));
61 | ds_H = freqz(ds_B, ds_A, iec_f, Fs);
62 |
63 | % Equalizer filter, i.e. inverse from estimated transfer filter
64 | % Swap A and B coefficients, and normalize to ds_B(1)
65 | eq_B = ds_A./ds_B(1)
66 | eq_A = ds_B./ds_B(1)
67 | % eq_B = 1.001240684967618 -1.996936108836719 0.995703101823296
68 | % eq_A = 1.000000000000000 -1.997675693595923 0.997677044195944
69 | eq_H = freqz(eq_B, eq_A, iec_f, Fs);
70 | % Check for poles ouside unit circle
71 | roots(eq_B)
72 | roots(eq_A)
73 |
74 |
75 | clf;
76 | semilogx(iec_f, ds_dB, 'g;IM69D130 Datasheet plot (approx.);');
77 | hold on;
78 | title("IM69D130 Adjusted Frequency Response");
79 | grid minor;
80 | xlabel('Frequency (Hz)');
81 | xlim([10, 24000]);
82 | ylabel('Amplitude (dB)');
83 | ylim([-20, 20]);
84 | legend ('boxoff');
85 | legend ('location', 'northwest');
86 | semilogx(iec_f, iec_c1_t_dB, '--r;IEC 61672-1:2013 Class 1 tolerance;');
87 | semilogx(iec_f, iec_c1_b_dB, '--r');
88 | semilogx(iec_f, iec_c2_t_dB, 'r;IEC 61672-1:2013 Class 2 tolerance;');
89 | semilogx(iec_f, iec_c2_b_dB, 'r');
90 | semilogx(iec_f, 20*log10(abs(ds_H)), '--c;IIR filter frequency response;');
91 | semilogx(iec_f, (ds_dB + 20*log10(abs(eq_H))), 'b;Adjusted frequency response;', 'linewidth', 1);
92 | hold off;
93 |
94 | [sos, gain] = tf2sos(eq_B, eq_A)
95 |
--------------------------------------------------------------------------------
/math/inmp441.m:
--------------------------------------------------------------------------------
1 | %
2 | % INMP441 Equalizer filter design
3 | %
4 | % (c)2019 Ivan Kostoski
5 | %
6 |
7 | clear;
8 | % Sampling frequency
9 | Fs = 48000;
10 |
11 | % IEC specified frequencies
12 | iec_f = [10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, ...
13 | 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, ...
14 | 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, ...
15 | 10000, 12500, 16000, 20000];
16 |
17 | % Values visually estimated from INMP441 datasheet 'Typical Frequency Response' plot
18 | % 10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, ...
19 | ds_dB = [ -inf, -inf, -inf, -12, -10, -8, -6, -4.4, -3.4, -2.5, ...
20 | -1.9, -1.4, -0.9, -0.5, -0.2, 0, 0, 0, 0, 0, ...
21 | 0, 0, 0, 0, 0, 0, -0.4, -0.7, -1.1, -1.8, ...
22 | -2.4, -3.0, -5.0, -inf];
23 |
24 | % These value are selected and adjusted for better curve fit
25 | ds_l_f = [ 20, 50, 100, 1000];
26 | ds_l_dB = [ -13, -4.1, -1.8, 0];
27 |
28 | % Low frequency filter design
29 | % Convert Hz in rad/s and normalize for Fs
30 | ds_l_w = ds_l_f.*((2*pi)/Fs);
31 | % Convert plot decibels to magnitude
32 | ds_l_mag = arrayfun(@db2mag, ds_l_dB);
33 | % Estimate coefficients
34 | [ds_l_B, ds_l_A] = invfreqz(ds_l_mag, ds_l_w, 2, 2);
35 | % Stabilize and normalize the filter
36 | ds_A = polystab(ds_l_A) * norm(ds_l_A) / norm(polystab(ds_l_A));
37 | ds_B = polystab(ds_l_B) * norm(ds_l_B) / norm(polystab(ds_l_B));
38 | ds_H = freqz(ds_B, ds_A, iec_f, Fs);
39 |
40 | % Equalizer filter, i.e. inverse from estimated transfer filter
41 | % Swap A and B coefficients, and normalize to ds_B(1)
42 | eq_B = ds_A./ds_B(1)
43 | eq_A = ds_B./ds_B(1)
44 | eq_H = freqz(eq_B, eq_A, iec_f, Fs);
45 |
46 | clf;
47 | figure(1, 'position', [0,0,800,500]);
48 | title("INMP441 Frequency response");
49 | grid minor;
50 | xlabel('Frequency (Hz)');
51 | xlim([10, 20000]);
52 | ylabel('Magnitude (dB)');
53 | ylim([-20, 20]);
54 | hold on;
55 | semilogx(iec_f, ds_dB, 'g;INMP441 Datasheet plot (approx.);');
56 | semilogx(iec_f, 20*log10(abs(ds_H)), '--c;IIR filter frequency response;');
57 | semilogx(iec_f, (ds_dB + 20*log10(abs(eq_H))), 'b;Adjusted frequency response;', 'linewidth', 3);
58 | legend ('boxoff');
59 | legend ('location', 'northwest');
60 | hold off;
61 |
62 | [sos, gain] = tf2sos(eq_B, eq_A)
--------------------------------------------------------------------------------
/math/sph645lm4h-b.m:
--------------------------------------------------------------------------------
1 | %
2 | % SPH0645LM4H-B Equalizer filter design
3 | %
4 | % (c)2019 Ivan Kostoski
5 | %
6 |
7 | clear;
8 | format long;
9 |
10 | % Sampling frequency
11 | Fs = 48000;
12 |
13 | % IEC specified frequencies
14 | iec_f = [10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, ...
15 | 100, 125, 160, 200, 250, 315, 400, 500, 630, 800, ...
16 | 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, ...
17 | 10000, 12500, 16000, 20000];
18 |
19 | % IEC Class 1 tolerances (top/bottom)
20 | iec_c1_t_dB = [+3.5, +3.0, +2.5, +2.5, +2.5, +2.0, +1.5, +1.5, +1.5, +1.5, ...
21 | +1.5, +1.5, +1.5, +1.5, +1.4, +1.4, +1.4, +1.4, +1.4, +1.4, ...
22 | +1.1, +1.4, +1.4, +1.6, +1.6, +1.6, +1.6, +2.1, +2.1, +2.1, ...
23 | +2.6, +3.0, +3.5, +4.0];
24 | iec_c1_b_dB = [-inf, -inf, -4.5, -2.5, -2.5, -2.0, -1.5, -1.5, -1.5, -1.5, ...
25 | -1.5, -1.5, -1.5, -1.5, -1.4, -1.4, -1.4, -1.4, -1.4, -1.4, ...
26 | -1.1, -1.4, -1.4, -1.6, -1.6, -1.6, -1.6, -2.1, -2.6, -3.1, ...
27 | -3.6, -6.0, -17, -inf];
28 |
29 | % IEC Class 2 tolerances (top/bottom)
30 | iec_c2_t_dB = [+5.5, +5.5, +5.5, +3.5, +3.5, +3.5, +2.5, +2.5, +2.5, +2.5, ...
31 | +2.0, +2.0, +2.0, +2.0, +1.9, +1.9, +1.9, +1.9, +1.9, +1.9, ...
32 | +1.4, +1.9, +2.6, +2.6, +3.1, +3.1, +3.6, +4.1, +5.1, +5.6, ...
33 | +6.0, +6.0, +6.0, +6.0];
34 | iec_c2_b_dB = [-inf, -inf, -inf, -3.5, -3.5, -3.5, -2.5, -2.5, -2.5, -2.5, ...
35 | -2.0, -2.0, -2.0, -2.0, -1.9, -1.9, -1.9, -1.9, -1.9, -1.9, ...
36 | -1.4, -1.9, -2.6, -2.6, -3.1, -3.1, -3.6, -4.1, -5.1, -5.6, ...
37 | -inf, -inf, -inf, -inf];
38 |
39 | % Values visually estimated from ICS-43434 datasheet 'Typical Frequency Response' plot
40 | % 10, 12.5, 16, 20, 25, 31.5, 40, 50, 63, 80, ...
41 | ds_dB = [ -inf, -inf, -inf, -7.5, -6.5, -5, -3.6, -2.8, -2, -1.3, ...
42 | -1, -0.7, -0.4, -0.3, -0.1, -0.1, 0, 0, 0, 0, ...
43 | 0, 0, 0, 0, +0.1, +0.1, +0.3, +0.5, +0.8, +0.9, ...
44 | +1, +0.8, +0, -5.5];
45 |
46 | % These value are selected and adjusted for better curve fit
47 | ds_l_f = [ 20, 50, 100, 1000];
48 | ds_l_dB = [-7.5, -2.8, -1, 0];
49 |
50 | % Low frequency filter design
51 | % Convert Hz in rad/s and normalize for Fs
52 | ds_l_wn = ds_l_f.*((2*pi)/Fs);
53 | % Convert plot decibels to magnitude
54 | ds_l_mag = arrayfun(@db2mag, ds_l_dB);
55 | % Estimate coefficients
56 | [ds_l_B, ds_l_A] = invfreqz(ds_l_mag, ds_l_wn, 2, 2);
57 | % Stabilize and normalize the filter
58 | ds_l_Bs = polystab(ds_l_B) * norm(ds_l_B) / norm(polystab(ds_l_B));
59 | ds_l_As = polystab(ds_l_A) * norm(ds_l_A) / norm(polystab(ds_l_A));
60 |
61 | % High frequency filter design
62 | %ds_h_f = [ 1000, 2000, 4000, 8000, 10000, 16000, 20000];
63 | %ds_h_dB = [ 0, 0, +0.3, +0.9, +1, 0, -5.5];
64 | %[ds_h_B, ds_h_A] = invfreqz(arrayfun(@db2mag, ds_h_dB), (ds_h_f.*((2*pi)/Fs)), 4, 3);
65 | %ds_h_Bs = polystab(ds_h_B) * norm(ds_h_B) / norm(polystab(ds_h_B));
66 | %ds_h_As = polystab(ds_h_A) * norm(ds_h_A) / norm(polystab(ds_h_A));
67 |
68 | % Convolve into single 4th order filter
69 | ds_B = ds_l_Bs % conv(ds_l_Bs, ds_h_Bs);
70 | ds_A = ds_l_As % conv(ds_l_As, ds_h_As);
71 | ds_H = freqz(ds_B, ds_A, iec_f, Fs);
72 |
73 | % Equalizer filter, i.e. inverse from estimated transfer filter
74 | % Swap A and B coefficients, and normalize to ds_B(1)
75 | eq_B = ds_A./ds_B(1)
76 | eq_A = ds_B./ds_B(1)
77 | % Check for poles ouside unit circle
78 | roots(eq_B)
79 | roots(eq_A)
80 |
81 | % Add DC blocking filter
82 | [sos, gain] = tf2sos(eq_B, eq_A)
83 | sos = [sos; [1.0, -1.0, 0.0, 1.0, -0.9992, 0]];
84 | [eq_B, eq_A] = sos2tf(sos, gain)
85 | eq_H = freqz(eq_B, eq_A, iec_f, Fs);
86 |
87 | clf;
88 | semilogx(iec_f, ds_dB, 'g;SPH0645LM4H-B Datasheet plot (approx.);');
89 | hold on;
90 | title("SPH0645LM4H-B Frequency response");
91 | grid minor;
92 | xlabel('Frequency (Hz)');
93 | xlim([10, 24000]);
94 | ylabel('Amplitude (dB)');
95 | ylim([-30, 20]);
96 | legend ('boxoff');
97 | legend ('location', 'northwest');
98 | semilogx(iec_f, iec_c1_t_dB, '--r;IEC 61672-1:2013 Class 1 tolerance;');
99 | semilogx(iec_f, iec_c1_b_dB, '--r');
100 | semilogx(iec_f, iec_c2_t_dB, 'r;IEC 61672-1:2013 Class 2 tolerance;');
101 | semilogx(iec_f, iec_c2_b_dB, 'r');
102 | semilogx(iec_f, 20*log10(abs(ds_H)), '--c;IIR filter frequency response;');
103 | semilogx(iec_f, (ds_dB + 20*log10(abs(eq_H))), 'b;Adjusted frequency response;', 'linewidth', 3);
104 | hold off;
105 |
106 | % Convert to Second-Order Sections
107 | [sos, gain] = tf2sos(eq_B, eq_A)
--------------------------------------------------------------------------------
/misc/ics-43434-afr.svg:
--------------------------------------------------------------------------------
1 |
2 |
698 |
699 |
--------------------------------------------------------------------------------
/sos-iir-filter.h:
--------------------------------------------------------------------------------
1 | /*
2 | * ESP32 Second-Order Sections IIR Filter implementation
3 | *
4 | * (c)2019 Ivan Kostoski
5 | *
6 | * This program is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * This program is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with this program. If not, see .
18 | */
19 |
20 | #ifndef SOS_IIR_FILTER_H
21 | #define SOS_IIR_FILTER_H
22 |
23 | #include
24 |
25 | struct SOS_Coefficients {
26 | float b1;
27 | float b2;
28 | float a1;
29 | float a2;
30 | };
31 |
32 | struct SOS_Delay_State {
33 | float w0 = 0;
34 | float w1 = 0;
35 | };
36 |
37 | extern "C" {
38 | int sos_filter_f32(float *input, float *output, int len, const SOS_Coefficients &coeffs, SOS_Delay_State &w);
39 | }
40 | __asm__ (
41 | //
42 | // ESP32 implementation of IIR Second-Order Section filter
43 | // Assumes a0 and b0 coefficients are one (1.0)
44 | //
45 | // float* a2 = input;
46 | // float* a3 = output;
47 | // int a4 = len;
48 | // float* a5 = coeffs;
49 | // float* a6 = w;
50 | // float a7 = gain;
51 | //
52 | ".text \n"
53 | ".align 4 \n"
54 | ".global sos_filter_f32 \n"
55 | ".type sos_filter_f32,@function\n"
56 | "sos_filter_f32: \n"
57 | " entry a1, 16 \n"
58 | " lsi f0, a5, 0 \n" // float f0 = coeffs.b1;
59 | " lsi f1, a5, 4 \n" // float f1 = coeffs.b2;
60 | " lsi f2, a5, 8 \n" // float f2 = coeffs.a1;
61 | " lsi f3, a5, 12 \n" // float f3 = coeffs.a2;
62 | " lsi f4, a6, 0 \n" // float f4 = w[0];
63 | " lsi f5, a6, 4 \n" // float f5 = w[1];
64 | " loopnez a4, 1f \n" // for (; len>0; len--) {
65 | " lsip f6, a2, 4 \n" // float f6 = *input++;
66 | " madd.s f6, f2, f4 \n" // f6 += f2 * f4; // coeffs.a1 * w0
67 | " madd.s f6, f3, f5 \n" // f6 += f3 * f5; // coeffs.a2 * w1
68 | " mov.s f7, f6 \n" // f7 = f6; // b0 assumed 1.0
69 | " madd.s f7, f0, f4 \n" // f7 += f0 * f4; // coeffs.b1 * w0
70 | " madd.s f7, f1, f5 \n" // f7 += f1 * f5; // coeffs.b2 * w1 -> result
71 | " ssip f7, a3, 4 \n" // *output++ = f7;
72 | " mov.s f5, f4 \n" // f5 = f4; // w1 = w0
73 | " mov.s f4, f6 \n" // f4 = f6; // w0 = f6
74 | " 1: \n" // }
75 | " ssi f4, a6, 0 \n" // w[0] = f4;
76 | " ssi f5, a6, 4 \n" // w[1] = f5;
77 | " movi.n a2, 0 \n" // return 0;
78 | " retw.n \n"
79 | );
80 |
81 | extern "C" {
82 | float sos_filter_sum_sqr_f32(float *input, float *output, int len, const SOS_Coefficients &coeffs, SOS_Delay_State &w, float gain);
83 | }
84 | __asm__ (
85 | //
86 | // ESP32 implementation of IIR Second-Order section filter with applied gain.
87 | // Assumes a0 and b0 coefficients are one (1.0)
88 | // Returns sum of squares of filtered samples
89 | //
90 | // float* a2 = input;
91 | // float* a3 = output;
92 | // int a4 = len;
93 | // float* a5 = coeffs;
94 | // float* a6 = w;
95 | // float a7 = gain;
96 | //
97 | ".text \n"
98 | ".align 4 \n"
99 | ".global sos_filter_sum_sqr_f32 \n"
100 | ".type sos_filter_sum_sqr_f32,@function \n"
101 | "sos_filter_sum_sqr_f32: \n"
102 | " entry a1, 16 \n"
103 | " lsi f0, a5, 0 \n" // float f0 = coeffs.b1;
104 | " lsi f1, a5, 4 \n" // float f1 = coeffs.b2;
105 | " lsi f2, a5, 8 \n" // float f2 = coeffs.a1;
106 | " lsi f3, a5, 12 \n" // float f3 = coeffs.a2;
107 | " lsi f4, a6, 0 \n" // float f4 = w[0];
108 | " lsi f5, a6, 4 \n" // float f5 = w[1];
109 | " wfr f6, a7 \n" // float f6 = gain;
110 | " const.s f10, 0 \n" // float sum_sqr = 0;
111 | " loopnez a4, 1f \n" // for (; len>0; len--) {
112 | " lsip f7, a2, 4 \n" // float f7 = *input++;
113 | " madd.s f7, f2, f4 \n" // f7 += f2 * f4; // coeffs.a1 * w0
114 | " madd.s f7, f3, f5 \n" // f7 += f3 * f5; // coeffs.a2 * w1;
115 | " mov.s f8, f7 \n" // f8 = f7; // b0 assumed 1.0
116 | " madd.s f8, f0, f4 \n" // f8 += f0 * f4; // coeffs.b1 * w0;
117 | " madd.s f8, f1, f5 \n" // f8 += f1 * f5; // coeffs.b2 * w1;
118 | " mul.s f9, f8, f6 \n" // f9 = f8 * f6; // f8 * gain -> result
119 | " ssip f9, a3, 4 \n" // *output++ = f9;
120 | " mov.s f5, f4 \n" // f5 = f4; // w1 = w0
121 | " mov.s f4, f7 \n" // f4 = f7; // w0 = f7;
122 | " madd.s f10, f9, f9 \n" // f10 += f9 * f9; // sum_sqr += f9 * f9;
123 | " 1: \n" // }
124 | " ssi f4, a6, 0 \n" // w[0] = f4;
125 | " ssi f5, a6, 4 \n" // w[1] = f5;
126 | " rfr a2, f10 \n" // return sum_sqr;
127 | " retw.n \n" //
128 | );
129 |
130 |
131 | /**
132 | * Envelops above asm functions into C++ class
133 | */
134 | struct SOS_IIR_Filter {
135 |
136 | const int num_sos;
137 | const float gain;
138 | SOS_Coefficients* sos = NULL;
139 | SOS_Delay_State* w = NULL;
140 |
141 | // Dynamic constructor
142 | SOS_IIR_Filter(size_t num_sos, const float gain, const SOS_Coefficients _sos[] = NULL): num_sos(num_sos), gain(gain) {
143 | if (num_sos > 0) {
144 | sos = new SOS_Coefficients[num_sos];
145 | if ((sos != NULL) && (_sos != NULL)) memcpy(sos, _sos, num_sos * sizeof(SOS_Coefficients));
146 | w = new SOS_Delay_State[num_sos]();
147 | }
148 | };
149 |
150 | // Template constructor for const filter declaration
151 | template
152 | SOS_IIR_Filter(const float gain, const SOS_Coefficients (&sos)[Array_Size]): SOS_IIR_Filter(Array_Size, gain, sos) {};
153 |
154 | /**
155 | * Apply defined IIR Filter to input array of floats, write filtered values to output,
156 | * and return sum of squares of all filtered values
157 | */
158 | inline float filter(float* input, float* output, size_t len) {
159 | if ((num_sos < 1) || (sos == NULL) || (w == NULL)) return 0;
160 | float* source = input;
161 | // Apply all but last Second-Order-Section
162 | for(int i=0; i<(num_sos-1); i++) {
163 | sos_filter_f32(source, output, len, sos[i], w[i]);
164 | source = output;
165 | }
166 | // Apply last SOS with gain and return the sum of squares of all samples
167 | return sos_filter_sum_sqr_f32(source, output, len, sos[num_sos-1], w[num_sos-1], gain);
168 | }
169 |
170 | ~SOS_IIR_Filter() {
171 | if (w != NULL) delete[] w;
172 | if (sos != NULL) delete[] sos;
173 | }
174 |
175 | };
176 |
177 | //
178 | // For testing only
179 | //
180 | struct No_IIR_Filter {
181 | const int num_sos = 0;
182 | const float gain = 1.0;
183 |
184 | No_IIR_Filter() {};
185 |
186 | inline float filter(float* input, float* output, size_t len) {
187 | float sum_sqr = 0;
188 | float s;
189 | for(int i=0; i