diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2019-09-19 00:44:18 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2019-09-19 00:44:18 +0300 |
commit | 3e33324de42366389678eca16250687b33aa5692 (patch) | |
tree | 3a262041f9c47df32b356baa936ebaa1a5cfab35 /tools/plot | |
parent | 1600f372e233413fa662a3526752a531d7bb7fdd (diff) | |
download | libpqf-3e33324de42366389678eca16250687b33aa5692.tar.gz |
Initial implementation of polyphase quadrature filterbank
Diffstat (limited to 'tools/plot')
-rw-r--r-- | tools/plot/data/README.md | 2 | ||||
-rw-r--r-- | tools/plot/data/at3plus.txt | 385 | ||||
-rw-r--r-- | tools/plot/data/mpc.txt | 513 | ||||
-rw-r--r-- | tools/plot/main.py | 158 | ||||
-rw-r--r-- | tools/plot/pqf/pqf_binding.c | 257 | ||||
-rw-r--r-- | tools/plot/pqf/setup.py | 6 | ||||
-rw-r--r-- | tools/plot/run.sh | 12 |
7 files changed, 1333 insertions, 0 deletions
diff --git a/tools/plot/data/README.md b/tools/plot/data/README.md new file mode 100644 index 0000000..e260a15 --- /dev/null +++ b/tools/plot/data/README.md @@ -0,0 +1,2 @@ +Examples of pqf prototype filters +mpc.txt - Musepack diff --git a/tools/plot/data/at3plus.txt b/tools/plot/data/at3plus.txt new file mode 100644 index 0000000..ec72aaf --- /dev/null +++ b/tools/plot/data/at3plus.txt @@ -0,0 +1,385 @@ +#ATRAC3PLUS prototype file +0.00000058336207, +0.00000080604229, +0.00000042005411, +0.00000004440057, +-0.00000003226247, +-0.00000003530856, +-0.00000001266038, +-0.00001051678282, +0.00001183861787, +-0.00000060053890, +-0.00000143337536, +-0.00000231086847, +-0.00000325697420, +-0.00000461924219, +-0.00000638942583, +-0.00000703029718, +-0.00000052268200, +-0.00000091714480, +-0.00000108977838, +-0.00000023649704, +0.00000027792686, +0.00000052588763, +0.00000046299544, +-0.00001084099222, +-0.00001220357626, +-0.00002196196510, +-0.00002134877286, +-0.00001990710552, +-0.00001734808211, +-0.00001198405243, +-0.00000727012593, +-0.00000629902070, +-0.00000916228237, +-0.00001050293486, +-0.00000792127867, +-0.00000417120236, +-0.00000263366292, +-0.00000154329177, +-0.00000057168614, +0.00000181119538, +0.00002353085074, +0.00002780561954, +0.00003230232323, +0.00003696891872, +0.00004157561489, +0.00004533784522, +0.00004604394780, +0.00004858558168, +0.00000991988691, +0.00001313118355, +0.00001478948616, +0.00001984130540, +0.00002653474803, +0.00003386474418, +0.00004157788862, +0.00004978773723, +0.00002968751687, +0.00003634074528, +0.00003947730875, +0.00004292758240, +0.00004713382441, +0.00005426778807, +0.00006174868031, +0.00006414738891, +0.00006446454790, +0.00006830695202, +0.00007308147178, +0.00007612784975, +0.00007485075184, +0.00007020850899, +0.00006228515122, +0.00005827044151, +0.00005629632869, +0.00004988881119, +0.00003561532503, +0.00001853294270, +-0.00000166573534, +-0.00002610587035, +-0.00005339706695, +-0.00008079566032, +-0.00011279431783, +-0.00014691354590, +-0.00018116166757, +-0.00021972040122, +-0.00025986303808, +-0.00030001782579, +-0.00033926189644, +-0.00036942670704, +-0.00042499689152, +-0.00045358928037, +-0.00048466061708, +-0.00051135791000, +-0.00053276919061, +-0.00054783461383, +-0.00055472925305, +-0.00055432377849, +-0.00054488552269, +-0.00052537227748, +-0.00049731286708, +-0.00045777999912, +-0.00040612387238, +-0.00034301576670, +-0.00026866336702, +-0.00018248900597, +-0.00008430792514, +0.00002508115722, +0.00014135583478, +0.00026649952633, +0.00039945056778, +0.00053928449051, +0.00068422866752, +0.00083093711874, +0.00097943725996, +0.00112581404392, +0.00126854737755, +0.00140469032340, +0.00153207418043, +0.00164810777642, +0.00175017165020, +0.00183808000293, +0.00189900479745, +0.00194506184198, +0.00196531717665, +0.00195937906392, +0.00192521407735, +0.00186084536836, +0.00176518992521, +0.00163768627681, +0.00147714314517, +0.00128322700039, +0.00105668208562, +0.00079780723900, +0.00050782406470, +0.00018855913368, +-0.00015771533072, +-0.00052769453032, +-0.00091862218687, +-0.00132635701448, +-0.00174694834277, +-0.00217548245564, +-0.00260676839389, +-0.00303528923541, +-0.00345493946224, +-0.00385913741775, +-0.00424182694405, +-0.00459594465792, +-0.00491465069354, +-0.00519145652652, +-0.00541948433965, +-0.00559201091528, +-0.00570259056985, +-0.00574458623305, +-0.00571426097304, +-0.00560342334211, +-0.00540914759040, +-0.00512683158740, +-0.00475297635421, +-0.00428478466347, +-0.00372032052837, +-0.00305867753923, +-0.00229951413348, +-0.00144354603253, +-0.00049266568385, +0.00055068987422, +0.00168289500289, +0.00289928726852, +0.00419431505725, +0.00556147377938, +0.00699351215735, +0.00848235655576, +0.01001896336675, +0.01159386243671, +0.01319687161595, +0.01481730863452, +0.01644404232502, +0.01806553266943, +0.01967016234994, +0.02124618366361, +0.02278177253902, +0.02426535822451, +0.02568554319441, +0.02703126519918, +0.02829195372760, +0.02945766039193, +0.03051890246570, +0.03146736323833, +0.03229522705078, +0.03299576789141, +0.03356324881315, +0.03399298712611, +0.03428143635392, +0.03442628309131, +0.03442628309131, +0.03428143635392, +0.03399298712611, +0.03356324881315, +0.03299576789141, +0.03229522705078, +0.03146736323833, +0.03051890246570, +0.02945766039193, +0.02829195372760, +0.02703126519918, +0.02568554319441, +0.02426535822451, +0.02278177253902, +0.02124618366361, +0.01967016234994, +0.01806553266943, +0.01644404232502, +0.01481730863452, +0.01319687161595, +0.01159386243671, +0.01001896336675, +0.00848235655576, +0.00699351215735, +0.00556147377938, +0.00419431505725, +0.00289928726852, +0.00168289500289, +0.00055068987422, +-0.00049266568385, +-0.00144354603253, +-0.00229951413348, +-0.00305867753923, +-0.00372032052837, +-0.00428478466347, +-0.00475297635421, +-0.00512683158740, +-0.00540914759040, +-0.00560342334211, +-0.00571426097304, +-0.00574458623305, +-0.00570259056985, +-0.00559201091528, +-0.00541948433965, +-0.00519145652652, +-0.00491465069354, +-0.00459594465792, +-0.00424182694405, +-0.00385913741775, +-0.00345493946224, +-0.00303528923541, +-0.00260676839389, +-0.00217548245564, +-0.00174694834277, +-0.00132635701448, +-0.00091862218687, +-0.00052769453032, +-0.00015771533072, +0.00018855913368, +0.00050782406470, +0.00079780723900, +0.00105668208562, +0.00128322700039, +0.00147714314517, +0.00163768627681, +0.00176518992521, +0.00186084536836, +0.00192521407735, +0.00195937906392, +0.00196531717665, +0.00194506184198, +0.00189900479745, +0.00183808000293, +0.00175017165020, +0.00164810777642, +0.00153207418043, +0.00140469032340, +0.00126854737755, +0.00112581404392, +0.00097943725996, +0.00083093711874, +0.00068422866752, +0.00053928449051, +0.00039945056778, +0.00026649952633, +0.00014135583478, +0.00002508115722, +-0.00008430792514, +-0.00018248900597, +-0.00026866336702, +-0.00034301576670, +-0.00040612387238, +-0.00045777999912, +-0.00049731286708, +-0.00052537227748, +-0.00054488552269, +-0.00055432377849, +-0.00055472925305, +-0.00054783461383, +-0.00053276919061, +-0.00051135791000, +-0.00048466061708, +-0.00045358928037, +-0.00042499689152, +-0.00036942670704, +-0.00033926189644, +-0.00030001782579, +-0.00025986303808, +-0.00021972040122, +-0.00018116166757, +-0.00014691354590, +-0.00011279431783, +-0.00008079566032, +-0.00005339706695, +-0.00002610587035, +-0.00000166573534, +0.00001853294270, +0.00003561532503, +0.00004988881119, +0.00005629632869, +0.00005827044151, +0.00006228515122, +0.00007020850899, +0.00007485075184, +0.00007612784975, +0.00007308147178, +0.00006830695202, +0.00006446454790, +0.00006414738891, +0.00006174868031, +0.00005426778807, +0.00004713382441, +0.00004292758240, +0.00003947730875, +0.00003634074528, +0.00002968751687, +0.00004978773723, +0.00004157788862, +0.00003386474418, +0.00002653474803, +0.00001984130540, +0.00001478948616, +0.00001313118355, +0.00000991988691, +0.00004858558168, +0.00004604394780, +0.00004533784522, +0.00004157561489, +0.00003696891872, +0.00003230232323, +0.00002780561954, +0.00002353085074, +0.00000181119538, +-0.00000057168614, +-0.00000154329177, +-0.00000263366292, +-0.00000417120236, +-0.00000792127867, +-0.00001050293486, +-0.00000916228237, +-0.00000629902070, +-0.00000727012593, +-0.00001198405243, +-0.00001734808211, +-0.00001990710552, +-0.00002134877286, +-0.00002196196510, +-0.00001220357626, +-0.00001084099222, +0.00000046299544, +0.00000052588763, +0.00000027792686, +-0.00000023649704, +-0.00000108977838, +-0.00000091714480, +-0.00000052268200, +-0.00000703029718, +-0.00000638942583, +-0.00000461924219, +-0.00000325697420, +-0.00000231086847, +-0.00000143337536, +-0.00000060053890, +0.00001183861787, +-0.00001051678282, +-0.00000001266038, +-0.00000003530856, +-0.00000003226247, +0.00000004440057, +0.00000042005411, +0.00000080604229, +0.00000058336207 diff --git a/tools/plot/data/mpc.txt b/tools/plot/data/mpc.txt new file mode 100644 index 0000000..71691ae --- /dev/null +++ b/tools/plot/data/mpc.txt @@ -0,0 +1,513 @@ +# mpc prototype file +0.00000000000000, +-0.00000047683716, +-0.00000047683716, +-0.00000047683716, +-0.00000047683716, +-0.00000047683716, +-0.00000047683716, +-0.00000095367432, +-0.00000095367432, +-0.00000095367432, +-0.00000095367432, +-0.00000143051147, +-0.00000143051147, +-0.00000190734863, +-0.00000190734863, +-0.00000238418579, +-0.00000238418579, +-0.00000286102295, +-0.00000333786011, +-0.00000333786011, +-0.00000381469727, +-0.00000429153442, +-0.00000476837158, +-0.00000524520874, +-0.00000619888306, +-0.00000667572021, +-0.00000762939453, +-0.00000810623169, +-0.00000905990601, +-0.00001001358032, +-0.00001144409180, +-0.00001239776611, +-0.00001382827759, +-0.00001478195190, +-0.00001668930054, +-0.00001811981201, +-0.00001955032349, +-0.00002145767212, +-0.00002336502075, +-0.00002527236938, +-0.00002765655518, +-0.00003004074097, +-0.00003242492676, +-0.00003480911255, +-0.00003767013550, +-0.00004053115845, +-0.00004339218140, +-0.00004625320435, +-0.00004959106445, +-0.00005292892456, +-0.00005578994751, +-0.00005960464478, +-0.00006294250488, +-0.00006628036499, +-0.00007009506226, +-0.00007343292236, +-0.00007677078247, +-0.00008058547974, +-0.00008392333984, +-0.00008726119995, +-0.00009059906006, +-0.00009346008301, +-0.00009632110596, +-0.00009918212891, +-0.00010156631470, +-0.00010395050049, +-0.00010585784912, +-0.00010728836060, +-0.00010824203491, +-0.00010871887207, +-0.00010871887207, +-0.00010824203491, +-0.00010681152344, +-0.00010538101196, +-0.00010251998901, +-0.00009918212891, +-0.00009536743164, +-0.00009012222290, +-0.00008440017700, +-0.00007772445679, +-0.00006961822510, +-0.00006055831909, +-0.00005054473877, +-0.00003957748413, +-0.00002717971802, +-0.00001382827759, +0.00000095367432, +0.00001716613770, +0.00003433227539, +0.00005292892456, +0.00007295608521, +0.00009393692017, +0.00011634826660, +0.00014019012451, +0.00016546249390, +0.00019121170044, +0.00021886825562, +0.00024747848511, +0.00027704238892, +0.00030755996704, +0.00033903121948, +0.00037145614624, +0.00040435791016, +0.00043821334839, +0.00047254562378, +0.00050735473633, +0.00054216384888, +0.00057697296143, +0.00061178207397, +0.00064659118652, +0.00068092346191, +0.00071430206299, +0.00074720382690, +0.00077915191650, +0.00080966949463, +0.00083875656128, +0.00086641311646, +0.00089168548584, +0.00091505050659, +0.00093555450439, +0.00095415115356, +0.00096893310547, +0.00098085403442, +0.00098943710327, +0.00099420547485, +0.00099515914917, +0.00099182128906, +0.00098371505737, +0.00097131729126, +0.00095367431641, +0.00093078613281, +0.00090265274048, +0.00086879730225, +0.00082921981812, +0.00078392028809, +0.00073194503784, +0.00067424774170, +0.00061035156250, +0.00053930282593, +0.00046253204346, +0.00037860870361, +0.00028848648071, +0.00019168853760, +0.00008821487427, +-0.00002145767212, +-0.00013732910156, +-0.00025987625122, +-0.00038814544678, +-0.00052213668823, +-0.00066184997559, +-0.00080680847168, +-0.00095653533936, +-0.00111103057861, +-0.00126981735229, +-0.00143241882324, +-0.00159788131714, +-0.00176668167114, +-0.00193738937378, +-0.00211000442505, +-0.00228309631348, +-0.00245714187622, +-0.00263071060181, +-0.00280332565308, +-0.00297403335571, +-0.00314188003540, +-0.00330686569214, +-0.00346708297729, +-0.00362253189087, +-0.00377178192139, +-0.00391435623169, +-0.00404882431030, +-0.00417470932007, +-0.00429058074951, +-0.00439596176147, +-0.00448989868164, +-0.00457048416138, +-0.00463819503784, +-0.00469112396240, +-0.00472831726074, +-0.00474882125854, +-0.00475215911865, +-0.00473737716675, +-0.00470304489136, +-0.00464916229248, +-0.00457382202148, +-0.00447702407837, +-0.00435781478882, +-0.00421524047852, +-0.00404930114746, +-0.00385856628418, +-0.00364303588867, +-0.00340175628662, +-0.00313472747803, +-0.00284147262573, +-0.00252151489258, +-0.00217485427856, +-0.00180053710938, +-0.00139951705933, +-0.00097131729126, +-0.00051593780518, +-0.00003337860107, +0.00047588348389, +0.00101184844971, +0.00157356262207, +0.00216150283813, +0.00277423858643, +0.00341129302979, +0.00407218933105, +0.00475645065308, +0.00546216964722, +0.00618934631348, +0.00693702697754, +0.00770330429077, +0.00848722457886, +0.00928783416748, +0.01010370254517, +0.01093339920044, +0.01177501678467, +0.01262760162354, +0.01348924636841, +0.01435852050781, +0.01523351669312, +0.01611280441284, +0.01699447631836, +0.01787614822388, +0.01875686645508, +0.01963424682617, +0.02050685882568, +0.02137231826782, +0.02222871780396, +0.02307415008545, +0.02390718460083, +0.02472543716431, +0.02552700042725, +0.02631092071533, +0.02707386016846, +0.02781534194946, +0.02853298187256, +0.02922487258911, +0.02989006042480, +0.03052663803101, +0.03113269805908, +0.03170680999756, +0.03224802017212, +0.03275489807129, +0.03322553634644, +0.03365993499756, +0.03405570983887, +0.03441286087036, +0.03473043441772, +0.03500699996948, +0.03524208068848, +0.03543519973755, +0.03558635711670, +0.03569412231445, +0.03575897216797, +0.03578090667725, +0.03575897216797, +0.03569412231445, +0.03558635711670, +0.03543519973755, +0.03524208068848, +0.03500699996948, +0.03473043441772, +0.03441286087036, +0.03405570983887, +0.03365993499756, +0.03322553634644, +0.03275489807129, +0.03224802017212, +0.03170680999756, +0.03113269805908, +0.03052663803101, +0.02989006042480, +0.02922487258911, +0.02853298187256, +0.02781534194946, +0.02707386016846, +0.02631092071533, +0.02552700042725, +0.02472543716431, +0.02390718460083, +0.02307415008545, +0.02222871780396, +0.02137231826782, +0.02050685882568, +0.01963424682617, +0.01875686645508, +0.01787614822388, +0.01699447631836, +0.01611280441284, +0.01523351669312, +0.01435852050781, +0.01348924636841, +0.01262760162354, +0.01177501678467, +0.01093339920044, +0.01010370254517, +0.00928783416748, +0.00848722457886, +0.00770330429077, +0.00693702697754, +0.00618934631348, +0.00546216964722, +0.00475645065308, +0.00407218933105, +0.00341129302979, +0.00277423858643, +0.00216150283813, +0.00157356262207, +0.00101184844971, +0.00047588348389, +-0.00003337860107, +-0.00051593780518, +-0.00097131729126, +-0.00139951705933, +-0.00180053710938, +-0.00217485427856, +-0.00252151489258, +-0.00284147262573, +-0.00313472747803, +-0.00340175628662, +-0.00364303588867, +-0.00385856628418, +-0.00404930114746, +-0.00421524047852, +-0.00435781478882, +-0.00447702407837, +-0.00457382202148, +-0.00464916229248, +-0.00470304489136, +-0.00473737716675, +-0.00475215911865, +-0.00474882125854, +-0.00472831726074, +-0.00469112396240, +-0.00463819503784, +-0.00457048416138, +-0.00448989868164, +-0.00439596176147, +-0.00429058074951, +-0.00417470932007, +-0.00404882431030, +-0.00391435623169, +-0.00377178192139, +-0.00362253189087, +-0.00346708297729, +-0.00330686569214, +-0.00314188003540, +-0.00297403335571, +-0.00280332565308, +-0.00263071060181, +-0.00245714187622, +-0.00228309631348, +-0.00211000442505, +-0.00193738937378, +-0.00176668167114, +-0.00159788131714, +-0.00143241882324, +-0.00126981735229, +-0.00111103057861, +-0.00095653533936, +-0.00080680847168, +-0.00066184997559, +-0.00052213668823, +-0.00038814544678, +-0.00025987625122, +-0.00013732910156, +-0.00002145767212, +0.00008821487427, +0.00019168853760, +0.00028848648071, +0.00037860870361, +0.00046253204346, +0.00053930282593, +0.00061035156250, +0.00067424774170, +0.00073194503784, +0.00078392028809, +0.00082921981812, +0.00086879730225, +0.00090265274048, +0.00093078613281, +0.00095367431641, +0.00097131729126, +0.00098371505737, +0.00099182128906, +0.00099515914917, +0.00099420547485, +0.00098943710327, +0.00098085403442, +0.00096893310547, +0.00095415115356, +0.00093555450439, +0.00091505050659, +0.00089168548584, +0.00086641311646, +0.00083875656128, +0.00080966949463, +0.00077915191650, +0.00074720382690, +0.00071430206299, +0.00068092346191, +0.00064659118652, +0.00061178207397, +0.00057697296143, +0.00054216384888, +0.00050735473633, +0.00047254562378, +0.00043821334839, +0.00040435791016, +0.00037145614624, +0.00033903121948, +0.00030755996704, +0.00027704238892, +0.00024747848511, +0.00021886825562, +0.00019121170044, +0.00016546249390, +0.00014019012451, +0.00011634826660, +0.00009393692017, +0.00007295608521, +0.00005292892456, +0.00003433227539, +0.00001716613770, +0.00000095367432, +-0.00001382827759, +-0.00002717971802, +-0.00003957748413, +-0.00005054473877, +-0.00006055831909, +-0.00006961822510, +-0.00007772445679, +-0.00008440017700, +-0.00009012222290, +-0.00009536743164, +-0.00009918212891, +-0.00010251998901, +-0.00010538101196, +-0.00010681152344, +-0.00010824203491, +-0.00010871887207, +-0.00010871887207, +-0.00010824203491, +-0.00010728836060, +-0.00010585784912, +-0.00010395050049, +-0.00010156631470, +-0.00009918212891, +-0.00009632110596, +-0.00009346008301, +-0.00009059906006, +-0.00008726119995, +-0.00008392333984, +-0.00008058547974, +-0.00007677078247, +-0.00007343292236, +-0.00007009506226, +-0.00006628036499, +-0.00006294250488, +-0.00005960464478, +-0.00005578994751, +-0.00005292892456, +-0.00004959106445, +-0.00004625320435, +-0.00004339218140, +-0.00004053115845, +-0.00003767013550, +-0.00003480911255, +-0.00003242492676, +-0.00003004074097, +-0.00002765655518, +-0.00002527236938, +-0.00002336502075, +-0.00002145767212, +-0.00001955032349, +-0.00001811981201, +-0.00001668930054, +-0.00001478195190, +-0.00001382827759, +-0.00001239776611, +-0.00001144409180, +-0.00001001358032, +-0.00000905990601, +-0.00000810623169, +-0.00000762939453, +-0.00000667572021, +-0.00000619888306, +-0.00000524520874, +-0.00000476837158, +-0.00000429153442, +-0.00000381469727, +-0.00000333786011, +-0.00000333786011, +-0.00000286102295, +-0.00000238418579, +-0.00000238418579, +-0.00000190734863, +-0.00000190734863, +-0.00000143051147, +-0.00000143051147, +-0.00000095367432, +-0.00000095367432, +-0.00000095367432, +-0.00000095367432, +-0.00000047683716, +-0.00000047683716, +-0.00000047683716, +-0.00000047683716, +-0.00000047683716, +-0.00000047683716 diff --git a/tools/plot/main.py b/tools/plot/main.py new file mode 100644 index 0000000..ddf0bce --- /dev/null +++ b/tools/plot/main.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import argparse +import sys +import os +import re + +import numpy as np +import matplotlib.pyplot as plt +import scipy.fftpack + +import pqf + +RUNS = 2 +SAMPLERATE = 44100 + +def show_proto(proto): + + fig = plt.figure() + + yf = scipy.fftpack.fft(proto) + + max_freq = SAMPLERATE // 2; + f_len = len(proto) // 2 + freq_step = max_freq / f_len; + + freq = np.empty(f_len) + + for i in range(f_len): + freq[i] = i * freq_step + + fig.add_subplot(111).plot(freq, 20 * np.log10(np.abs(yf[:f_len]))) + fig.add_subplot(222).plot(proto) + + plt.show() + +def calc_energy_db(data): + res = 0.0 + for x in data: + res += x * x + + return 10 * np.log10(res / len(data)) + +def do_an(ctx, time, data, shift): + data_slice = data[shift : ctx.frame_sz() + shift] + time_slice = time[shift : ctx.frame_sz() + shift] + + energy_in = calc_energy_db(data_slice) + + res = ctx.do(data_slice) + + subbands_num = ctx.subbands_num(); + subband_sz = ctx.subband_sz(); + + energy_out = calc_energy_db(res) + + rel = energy_out - energy_in + + attenuations = [] + for i in range(subbands_num): + start = i * subband_sz + end = start + subband_sz + tmp = calc_energy_db(res[start:end]) - energy_in + for j in range(subband_sz): + attenuations.append(tmp) + + textstr = 'out - in : %.3f' % rel + + fig = plt.figure() + + color = 'tab:blue' + a = fig.add_subplot(111) + a.set_ylabel('value', color=color) + a.plot(res, color=color) + a.set_title("output") + props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) + a.text(0.01, 0.95, textstr, transform=a.transAxes, fontsize=10, + verticalalignment='top', bbox=props) + + ax2 = a.twinx() + color = 'tab:red' + ax2.set_ylabel('attenuation, db', color=color) + ax2.plot(attenuations, color=color) + + b = fig.add_subplot(222) + + textstr = 'in: %.3f' % energy_in + b.plot(time_slice, data_slice) + b.text(0.5, 0.95, textstr, transform=a.transAxes, fontsize=10, + verticalalignment='top', bbox=props) + + b.set_title("input"); + + plt.show() + +def process_freq(freq, subband_sz, subbands_num, prototype): + ctx = pqf.AnalyzeCtx(subband_sz, subbands_num, prototype) + frame_sz = ctx.frame_sz() + time = np.arange(0, frame_sz * RUNS, 1) + amplitude = 1 * np.sin(2 * np.pi * freq * time / SAMPLERATE).astype(np.float32) + + do_an(ctx, time, amplitude, 0) + do_an(ctx, time, amplitude, frame_sz) + +def show_response(pos, subband_sz, subbands_num, prototype): + ctx = pqf.AnalyzeCtx(subband_sz, subbands_num, prototype) + + time = np.arange(0, ctx.frame_sz() * RUNS, 1) + amplitude = np.zeros(ctx.frame_sz() * RUNS) + amplitude[ctx.frame_sz() + pos] = 1.0 + + do_an(ctx, time, amplitude, 0) + do_an(ctx, time, amplitude, ctx.frame_sz()) + +def load_prototype(path): + prototype = [] + with open(path) as fp: + for line in fp: + if line[0] == "#": + continue + for val in re.findall(r"[-+]?\d*\.\d+|\d+",line): + prototype.append(np.float32(val)) + + return prototype + +def main(): + parser = argparse.ArgumentParser() + + modes = ['freq', 'delta', 'proto'] + parser.add_argument('--mode', choices=modes, required=True, help='run mode') + parser.add_argument('--file', type=str, help='path to prototype file', required=True, action='store') + parser.add_argument('--subband-size', type=int, help='size of subband', action='store') + parser.add_argument('--subbands-num', type=int, help='number of subbands to split', action='store') + + args, free_args = parser.parse_known_args() + + if not os.path.isfile(args.file): + print("Prototype file {} does not exist.".format(args.file), file=sys.stderr) + sys.exit() + + prototype = load_prototype(args.file) + + if (args.mode == 'proto'): + show_proto(prototype) + if (args.mode == 'delta'): + if len(free_args) != 1: + print('one free argument (position) must be provided in delta mode') + sys.exit() + show_response(int(free_args[0]), args.subband_size, args.subbands_num, prototype) + else: + if len(free_args) != 1: + print('one free argument (frequency) must be provided in frequency mode') + sys.exit() + process_freq(int(free_args[0]), args.subband_size, args.subbands_num, prototype) + +if __name__ == "__main__": + main() diff --git a/tools/plot/pqf/pqf_binding.c b/tools/plot/pqf/pqf_binding.c new file mode 100644 index 0000000..353e2de --- /dev/null +++ b/tools/plot/pqf/pqf_binding.c @@ -0,0 +1,257 @@ +#include <Python.h> +#include <structmember.h> + +#include <libpqf.h> + +typedef struct { + PyObject_HEAD; + pqf_a_ctx_t ctx; +} AnalyzeCtxObject; + +static PyObject * +AnalyzeCtx_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *seq; + AnalyzeCtxObject *self; + uint32_t i; + float* proto; + uint32_t proto_sz; + uint32_t subband_sz; + uint32_t subbands_num; + + self = (AnalyzeCtxObject*)type->tp_alloc(type, 0); + + if (!self) { + return PyErr_NoMemory( ); + } + + if (!PyArg_ParseTuple(args, "iiO", + &subband_sz, &subbands_num, &seq)) + { + return NULL; + } + + seq = PySequence_Fast(seq, "argument must be iterable"); + + if (!seq) { + return NULL; + } + + proto_sz = PySequence_Fast_GET_SIZE(seq); + + proto = (float*)malloc(proto_sz * sizeof(float)); + + if(!proto) { + Py_DECREF(seq); + return PyErr_NoMemory( ); + } + + for (i = 0; i < proto_sz; i++) { + PyObject *fitem; + PyObject *item = PySequence_Fast_GET_ITEM(seq, i); + if(!item) { + Py_DECREF(seq); + free(proto); + return NULL; + } + + fitem = PyNumber_Float(item); + if(!fitem) { + Py_DECREF(seq); + free(proto); + PyErr_SetString(PyExc_TypeError, "all items must be numbers"); + return NULL; + } + + proto[i] = PyFloat_AS_DOUBLE(fitem); + Py_DECREF(fitem); + } + + pqf_status_t status = pqf_create_a_ctx(subband_sz, subbands_num, proto_sz, + proto, &self->ctx); + + free(proto); + + if (status != PQF_SUCCESS) { + switch (status) { + case PQF_NOMEM: + PyErr_SetString(PyExc_MemoryError, "No memory to create analyze context"); + break; + case PQF_WRONG_SUBBAND_SZ: + PyErr_SetString(PyExc_ValueError, "Unsupported subband size"); + break; + case PQF_WRONG_SUBBANDS_NUM: + PyErr_SetString(PyExc_ValueError, "Unsupported subbands number"); + break; + case PQF_WRONG_PROTO_SZ: + PyErr_SetString(PyExc_ValueError, "Unsupported prototype size"); + break; + case PQF_FRAME_TOO_LONG: + PyErr_SetString(PyExc_ValueError, "Frame too long"); + break; + case PQF_CONTRACT_VIOLATION: + PyErr_SetString(PyExc_ValueError, "Conract violation"); + break; + } + return NULL; + } + + return (PyObject*)self; +} + +static void +AnalyzeCtx_dealloc(AnalyzeCtxObject *self) +{ + pqf_free_a_ctx(self->ctx); +} + +static PyObject* analyzectx_framesz(PyObject* self, PyObject* args) +{ + AnalyzeCtxObject* ctx = (AnalyzeCtxObject*)self; + return PyLong_FromUnsignedLong(pqf_get_frame_sz(ctx->ctx)); +} + +static PyObject* analyzectx_subbandsz(PyObject* self, PyObject* args) +{ + AnalyzeCtxObject* ctx = (AnalyzeCtxObject*)self; + return PyLong_FromUnsignedLong(pqf_get_subband_sz(ctx->ctx)); +} + +static PyObject* analyzectx_subbandsnum(PyObject* self, PyObject* args) +{ + AnalyzeCtxObject* ctx = (AnalyzeCtxObject*)self; + return PyLong_FromUnsignedLong(pqf_get_subbands_num(ctx->ctx)); +} + +static PyObject* analyzectx_do(PyObject* self, PyObject* args) +{ + float* in; + float* out; + + uint32_t i = 0; + uint32_t len = 0; + AnalyzeCtxObject* ctx = (AnalyzeCtxObject*)self; + uint32_t frame_sz = pqf_get_frame_sz(ctx->ctx); + + PyObject *seq; + + if (!PyArg_ParseTuple(args, "O", &seq)) { + return NULL; + } + + seq = PySequence_Fast(seq, "argument must be iterable"); + + if (!seq) { + return NULL; + } + + len = PySequence_Fast_GET_SIZE(seq); + + if (len != frame_sz) { + PyErr_SetString(PyExc_ValueError, "wrong given frame size"); + return NULL; + } + + in = (float*)malloc(len * sizeof(float)); + if(!in) { + Py_DECREF(seq); + return PyErr_NoMemory( ); + } + + for (i = 0; i < len; i++) { + PyObject *fitem; + PyObject *item = PySequence_Fast_GET_ITEM(seq, i); + if(!item) { + Py_DECREF(seq); + free(in); + return NULL; + } + fitem = PyNumber_Float(item); + if(!fitem) { + Py_DECREF(seq); + free(in); + PyErr_SetString(PyExc_TypeError, "all items must be numbers"); + return NULL; + } + in[i] = PyFloat_AS_DOUBLE(fitem); + Py_DECREF(fitem); + } + + PyObject *my_list = PyList_New(0); + if(my_list == NULL) { + Py_DECREF(seq); + free(in); + return NULL; + } + + out = (float*)calloc(pqf_get_frame_sz(ctx->ctx), sizeof(float)); + if (out == NULL) { + Py_DECREF(seq); + free(in); + return NULL; + } + + + pqf_do_analyse(ctx->ctx, in, out); + + for (i = 0; i < len; i++) { + PyObject *o = Py_BuildValue("f", out[i]); + if(PyList_Append(my_list, o) == -1) { + Py_DECREF(seq); + free(in); + free(out); + return NULL; + } + } + + free(in); + free(out); + return my_list; +} + +static PyMethodDef methods[] = { + { NULL, NULL, 0, NULL } +}; + +static PyMethodDef AnalyzeCtx_methods[] = { + {"do", (PyCFunction)analyzectx_do, METH_VARARGS, NULL}, + {"frame_sz", (PyCFunction)analyzectx_framesz, METH_NOARGS, NULL}, + {"subband_sz", (PyCFunction)analyzectx_subbandsz, METH_NOARGS, NULL}, + {"subbands_num", (PyCFunction)analyzectx_subbandsnum, METH_NOARGS, NULL}, + {NULL} +}; + +static PyTypeObject AnalyzeCtxType = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = "pqf.AnalyzeCtx", + .tp_doc = "AnalyzeCtx", + .tp_basicsize = sizeof(AnalyzeCtxObject), + .tp_itemsize = 0, + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + .tp_new = AnalyzeCtx_new, + .tp_dealloc = (destructor)AnalyzeCtx_dealloc, + .tp_methods = AnalyzeCtx_methods, +}; + +static struct PyModuleDef module = { + PyModuleDef_HEAD_INIT, + .m_name = "pqf", + .m_doc = "pqf library test binding", + .m_size = -1, + .m_methods = methods +}; + +PyMODINIT_FUNC PyInit_pqf(void) +{ + PyObject *m; + if (PyType_Ready(&AnalyzeCtxType) < 0) + return NULL; + + m = PyModule_Create(&module); + if (m == NULL) + return NULL; + + Py_INCREF(&AnalyzeCtxType); + PyModule_AddObject(m, "AnalyzeCtx", (PyObject*)&AnalyzeCtxType); + return m; +} diff --git a/tools/plot/pqf/setup.py b/tools/plot/pqf/setup.py new file mode 100644 index 0000000..8ccd042 --- /dev/null +++ b/tools/plot/pqf/setup.py @@ -0,0 +1,6 @@ +from distutils.core import setup, Extension + +setup(name = 'pqf', version = '1.0', \ + ext_modules = [Extension('pqf', \ + sources=['pqf_binding.c', '../../../src/pqf.c'], \ + include_dirs=['../../../include/'])]) diff --git a/tools/plot/run.sh b/tools/plot/run.sh new file mode 100644 index 0000000..06e7788 --- /dev/null +++ b/tools/plot/run.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +set -e + +cd pqf + +python3.7 setup.py build + +cd .. + +export PYTHONPATH=./pqf/build/lib.mingw-3.7/ +./main.py "$@" |