Spaces:
Configuration error
Configuration error
File size: 44,694 Bytes
4a367ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Constant-Q transforms"""
import warnings
import numpy as np
from numba import jit
from . import audio
from .intervals import interval_frequencies
from .fft import get_fftlib
from .convert import cqt_frequencies, note_to_hz
from .spectrum import stft, istft
from .pitch import estimate_tuning
from .._cache import cache
from .. import filters
from .. import util
from ..util.exceptions import ParameterError
from numpy.typing import DTypeLike
from typing import Optional, Union, Collection, List
from .._typing import _WindowSpec, _PadMode, _FloatLike_co, _ensure_not_reachable
__all__ = ["cqt", "hybrid_cqt", "pseudo_cqt", "icqt", "griffinlim_cqt", "vqt"]
# TODO: ivqt, griffinlim_vqt
@cache(level=20)
def cqt(
y: np.ndarray,
*,
sr: float = 22050,
hop_length: int = 512,
fmin: Optional[_FloatLike_co] = None,
n_bins: int = 84,
bins_per_octave: int = 12,
tuning: Optional[float] = 0.0,
filter_scale: float = 1,
norm: Optional[float] = 1,
sparsity: float = 0.01,
window: _WindowSpec = "hann",
scale: bool = True,
pad_mode: _PadMode = "constant",
res_type: Optional[str] = "soxr_hq",
dtype: Optional[DTypeLike] = None,
) -> np.ndarray:
"""Compute the constant-Q transform of an audio signal.
This implementation is based on the recursive sub-sampling method
described by [#]_.
.. [#] Schoerkhuber, Christian, and Anssi Klapuri.
"Constant-Q transform toolbox for music processing."
7th Sound and Music Computing Conference, Barcelona, Spain. 2010.
Parameters
----------
y : np.ndarray [shape=(..., n)]
audio time series. Multi-channel is supported.
sr : number > 0 [scalar]
sampling rate of ``y``
hop_length : int > 0 [scalar]
number of samples between successive CQT columns.
fmin : float > 0 [scalar]
Minimum frequency. Defaults to `C1 ~= 32.70 Hz`
n_bins : int > 0 [scalar]
Number of frequency bins, starting at ``fmin``
bins_per_octave : int > 0 [scalar]
Number of bins per octave
tuning : None or float
Tuning offset in fractions of a bin.
If ``None``, tuning will be automatically estimated from the signal.
The minimum frequency of the resulting CQT will be modified to
``fmin * 2**(tuning / bins_per_octave)``.
filter_scale : float > 0
Filter scale factor. Small values (<1) use shorter windows
for improved time resolution.
norm : {inf, -inf, 0, float > 0}
Type of norm to use for basis function normalization.
See `librosa.util.normalize`.
sparsity : float in [0, 1)
Sparsify the CQT basis by discarding up to ``sparsity``
fraction of the energy in each basis.
Set ``sparsity=0`` to disable sparsification.
window : str, tuple, number, or function
Window specification for the basis filters.
See `filters.get_window` for details.
scale : bool
If ``True``, scale the CQT response by square-root the length of
each channel's filter. This is analogous to ``norm='ortho'`` in FFT.
If ``False``, do not scale the CQT. This is analogous to
``norm=None`` in FFT.
pad_mode : string
Padding mode for centered frame analysis.
See also: `librosa.stft` and `numpy.pad`.
res_type : string
The resampling mode for recursive downsampling.
dtype : np.dtype
The (complex) data type of the output array. By default, this is inferred to match
the numerical precision of the input signal.
Returns
-------
CQT : np.ndarray [shape=(..., n_bins, t)]
Constant-Q value each frequency at each time.
See Also
--------
vqt
librosa.resample
librosa.util.normalize
Notes
-----
This function caches at level 20.
Examples
--------
Generate and plot a constant-Q power spectrum
>>> import matplotlib.pyplot as plt
>>> y, sr = librosa.load(librosa.ex('trumpet'))
>>> C = np.abs(librosa.cqt(y, sr=sr))
>>> fig, ax = plt.subplots()
>>> img = librosa.display.specshow(librosa.amplitude_to_db(C, ref=np.max),
... sr=sr, x_axis='time', y_axis='cqt_note', ax=ax)
>>> ax.set_title('Constant-Q power spectrum')
>>> fig.colorbar(img, ax=ax, format="%+2.0f dB")
Limit the frequency range
>>> C = np.abs(librosa.cqt(y, sr=sr, fmin=librosa.note_to_hz('C2'),
... n_bins=60))
>>> C
array([[6.830e-04, 6.361e-04, ..., 7.362e-09, 9.102e-09],
[5.366e-04, 4.818e-04, ..., 8.953e-09, 1.067e-08],
...,
[4.288e-02, 4.580e-01, ..., 1.529e-05, 5.572e-06],
[2.965e-03, 1.508e-01, ..., 8.965e-06, 1.455e-05]])
Using a higher frequency resolution
>>> C = np.abs(librosa.cqt(y, sr=sr, fmin=librosa.note_to_hz('C2'),
... n_bins=60 * 2, bins_per_octave=12 * 2))
>>> C
array([[5.468e-04, 5.382e-04, ..., 5.911e-09, 6.105e-09],
[4.118e-04, 4.014e-04, ..., 7.788e-09, 8.160e-09],
...,
[2.780e-03, 1.424e-01, ..., 4.225e-06, 2.388e-05],
[5.147e-02, 6.959e-02, ..., 1.694e-05, 5.811e-06]])
"""
# CQT is the special case of VQT with gamma=0
return vqt(
y=y,
sr=sr,
hop_length=hop_length,
fmin=fmin,
n_bins=n_bins,
intervals="equal",
gamma=0,
bins_per_octave=bins_per_octave,
tuning=tuning,
filter_scale=filter_scale,
norm=norm,
sparsity=sparsity,
window=window,
scale=scale,
pad_mode=pad_mode,
res_type=res_type,
dtype=dtype,
)
@cache(level=20)
def hybrid_cqt(
y: np.ndarray,
*,
sr: float = 22050,
hop_length: int = 512,
fmin: Optional[_FloatLike_co] = None,
n_bins: int = 84,
bins_per_octave: int = 12,
tuning: Optional[float] = 0.0,
filter_scale: float = 1,
norm: Optional[float] = 1,
sparsity: float = 0.01,
window: _WindowSpec = "hann",
scale: bool = True,
pad_mode: _PadMode = "constant",
res_type: str = "soxr_hq",
dtype: Optional[DTypeLike] = None,
) -> np.ndarray:
"""Compute the hybrid constant-Q transform of an audio signal.
Here, the hybrid CQT uses the pseudo CQT for higher frequencies where
the hop_length is longer than half the filter length and the full CQT
for lower frequencies.
Parameters
----------
y : np.ndarray [shape=(..., n)]
audio time series. Multi-channel is supported.
sr : number > 0 [scalar]
sampling rate of ``y``
hop_length : int > 0 [scalar]
number of samples between successive CQT columns.
fmin : float > 0 [scalar]
Minimum frequency. Defaults to `C1 ~= 32.70 Hz`
n_bins : int > 0 [scalar]
Number of frequency bins, starting at ``fmin``
bins_per_octave : int > 0 [scalar]
Number of bins per octave
tuning : None or float
Tuning offset in fractions of a bin.
If ``None``, tuning will be automatically estimated from the signal.
The minimum frequency of the resulting CQT will be modified to
``fmin * 2**(tuning / bins_per_octave)``.
filter_scale : float > 0
Filter filter_scale factor. Larger values use longer windows.
norm : {inf, -inf, 0, float > 0}
Type of norm to use for basis function normalization.
See `librosa.util.normalize`.
sparsity : float in [0, 1)
Sparsify the CQT basis by discarding up to ``sparsity``
fraction of the energy in each basis.
Set ``sparsity=0`` to disable sparsification.
window : str, tuple, number, or function
Window specification for the basis filters.
See `filters.get_window` for details.
scale : bool
If ``True``, scale the CQT response by square-root the length of
each channel's filter. This is analogous to ``norm='ortho'`` in FFT.
If ``False``, do not scale the CQT. This is analogous to
``norm=None`` in FFT.
pad_mode : string
Padding mode for centered frame analysis.
See also: `librosa.stft` and `numpy.pad`.
res_type : string
Resampling mode. See `librosa.cqt` for details.
dtype : np.dtype, optional
The complex dtype to use for computing the CQT.
By default, this is inferred to match the precision of
the input signal.
Returns
-------
CQT : np.ndarray [shape=(..., n_bins, t), dtype=np.float]
Constant-Q energy for each frequency at each time.
See Also
--------
cqt
pseudo_cqt
Notes
-----
This function caches at level 20.
"""
if fmin is None:
# C1 by default
fmin = note_to_hz("C1")
if tuning is None:
tuning = estimate_tuning(y=y, sr=sr, bins_per_octave=bins_per_octave)
# Apply tuning correction
fmin = fmin * 2.0 ** (tuning / bins_per_octave)
# Get all CQT frequencies
freqs = cqt_frequencies(n_bins, fmin=fmin, bins_per_octave=bins_per_octave)
# Compute an alpha parameter, just in case we need it
alpha = __bpo_to_alpha(bins_per_octave)
# Compute the length of each constant-Q basis function
lengths, _ = filters.wavelet_lengths(
freqs=freqs, sr=sr, filter_scale=filter_scale, window=window, alpha=alpha
)
# Determine which filters to use with Pseudo CQT
# These are the ones that fit within 2 hop lengths after padding
pseudo_filters = 2.0 ** np.ceil(np.log2(lengths)) < 2 * hop_length
n_bins_pseudo = int(np.sum(pseudo_filters))
n_bins_full = n_bins - n_bins_pseudo
cqt_resp = []
if n_bins_pseudo > 0:
fmin_pseudo = np.min(freqs[pseudo_filters])
cqt_resp.append(
pseudo_cqt(
y,
sr=sr,
hop_length=hop_length,
fmin=fmin_pseudo,
n_bins=n_bins_pseudo,
bins_per_octave=bins_per_octave,
filter_scale=filter_scale,
norm=norm,
sparsity=sparsity,
window=window,
scale=scale,
pad_mode=pad_mode,
dtype=dtype,
)
)
if n_bins_full > 0:
cqt_resp.append(
np.abs(
cqt(
y,
sr=sr,
hop_length=hop_length,
fmin=fmin,
n_bins=n_bins_full,
bins_per_octave=bins_per_octave,
filter_scale=filter_scale,
norm=norm,
sparsity=sparsity,
window=window,
scale=scale,
pad_mode=pad_mode,
res_type=res_type,
dtype=dtype,
)
)
)
# Propagate dtype from the last component
return __trim_stack(cqt_resp, n_bins, cqt_resp[-1].dtype)
@cache(level=20)
def pseudo_cqt(
y: np.ndarray,
*,
sr: float = 22050,
hop_length: int = 512,
fmin: Optional[_FloatLike_co] = None,
n_bins: int = 84,
bins_per_octave: int = 12,
tuning: Optional[float] = 0.0,
filter_scale: float = 1,
norm: Optional[float] = 1,
sparsity: float = 0.01,
window: _WindowSpec = "hann",
scale: bool = True,
pad_mode: _PadMode = "constant",
dtype: Optional[DTypeLike] = None,
) -> np.ndarray:
"""Compute the pseudo constant-Q transform of an audio signal.
This uses a single fft size that is the smallest power of 2 that is greater
than or equal to the max of:
1. The longest CQT filter
2. 2x the hop_length
Parameters
----------
y : np.ndarray [shape=(..., n)]
audio time series. Multi-channel is supported.
sr : number > 0 [scalar]
sampling rate of ``y``
hop_length : int > 0 [scalar]
number of samples between successive CQT columns.
fmin : float > 0 [scalar]
Minimum frequency. Defaults to `C1 ~= 32.70 Hz`
n_bins : int > 0 [scalar]
Number of frequency bins, starting at ``fmin``
bins_per_octave : int > 0 [scalar]
Number of bins per octave
tuning : None or float
Tuning offset in fractions of a bin.
If ``None``, tuning will be automatically estimated from the signal.
The minimum frequency of the resulting CQT will be modified to
``fmin * 2**(tuning / bins_per_octave)``.
filter_scale : float > 0
Filter filter_scale factor. Larger values use longer windows.
norm : {inf, -inf, 0, float > 0}
Type of norm to use for basis function normalization.
See `librosa.util.normalize`.
sparsity : float in [0, 1)
Sparsify the CQT basis by discarding up to ``sparsity``
fraction of the energy in each basis.
Set ``sparsity=0`` to disable sparsification.
window : str, tuple, number, or function
Window specification for the basis filters.
See `filters.get_window` for details.
scale : bool
If ``True``, scale the CQT response by square-root the length of
each channel's filter. This is analogous to ``norm='ortho'`` in FFT.
If ``False``, do not scale the CQT. This is analogous to
``norm=None`` in FFT.
pad_mode : string
Padding mode for centered frame analysis.
See also: `librosa.stft` and `numpy.pad`.
dtype : np.dtype, optional
The complex data type for CQT calculations.
By default, this is inferred to match the precision of the input signal.
Returns
-------
CQT : np.ndarray [shape=(..., n_bins, t), dtype=np.float]
Pseudo Constant-Q energy for each frequency at each time.
Notes
-----
This function caches at level 20.
"""
if fmin is None:
# C1 by default
fmin = note_to_hz("C1")
if tuning is None:
tuning = estimate_tuning(y=y, sr=sr, bins_per_octave=bins_per_octave)
if dtype is None:
dtype = util.dtype_r2c(y.dtype)
# Apply tuning correction
fmin = fmin * 2.0 ** (tuning / bins_per_octave)
freqs = cqt_frequencies(fmin=fmin, n_bins=n_bins, bins_per_octave=bins_per_octave)
alpha = __bpo_to_alpha(bins_per_octave)
lengths, _ = filters.wavelet_lengths(
freqs=freqs, sr=sr, window=window, filter_scale=filter_scale, alpha=alpha
)
fft_basis, n_fft, _ = __vqt_filter_fft(
sr,
freqs,
filter_scale,
norm,
sparsity,
hop_length=hop_length,
window=window,
dtype=dtype,
alpha=alpha,
)
fft_basis = np.abs(fft_basis)
# Compute the magnitude-only CQT response
C: np.ndarray = __cqt_response(
y,
n_fft,
hop_length,
fft_basis,
pad_mode,
window="hann",
dtype=dtype,
phase=False,
)
if scale:
C /= np.sqrt(n_fft)
else:
# reshape lengths to match dimension properly
lengths = util.expand_to(lengths, ndim=C.ndim, axes=-2)
C *= np.sqrt(lengths / n_fft)
return C
@cache(level=40)
def icqt(
C: np.ndarray,
*,
sr: float = 22050,
hop_length: int = 512,
fmin: Optional[_FloatLike_co] = None,
bins_per_octave: int = 12,
tuning: float = 0.0,
filter_scale: float = 1,
norm: Optional[float] = 1,
sparsity: float = 0.01,
window: _WindowSpec = "hann",
scale: bool = True,
length: Optional[int] = None,
res_type: str = "soxr_hq",
dtype: Optional[DTypeLike] = None,
) -> np.ndarray:
"""Compute the inverse constant-Q transform.
Given a constant-Q transform representation ``C`` of an audio signal ``y``,
this function produces an approximation ``y_hat``.
Parameters
----------
C : np.ndarray, [shape=(..., n_bins, n_frames)]
Constant-Q representation as produced by `cqt`
sr : number > 0 [scalar]
sampling rate of the signal
hop_length : int > 0 [scalar]
number of samples between successive frames
fmin : float > 0 [scalar]
Minimum frequency. Defaults to `C1 ~= 32.70 Hz`
bins_per_octave : int > 0 [scalar]
Number of bins per octave
tuning : float [scalar]
Tuning offset in fractions of a bin.
The minimum frequency of the CQT will be modified to
``fmin * 2**(tuning / bins_per_octave)``.
filter_scale : float > 0 [scalar]
Filter scale factor. Small values (<1) use shorter windows
for improved time resolution.
norm : {inf, -inf, 0, float > 0}
Type of norm to use for basis function normalization.
See `librosa.util.normalize`.
sparsity : float in [0, 1)
Sparsify the CQT basis by discarding up to ``sparsity``
fraction of the energy in each basis.
Set ``sparsity=0`` to disable sparsification.
window : str, tuple, number, or function
Window specification for the basis filters.
See `filters.get_window` for details.
scale : bool
If ``True``, scale the CQT response by square-root the length
of each channel's filter. This is analogous to ``norm='ortho'`` in FFT.
If ``False``, do not scale the CQT. This is analogous to ``norm=None``
in FFT.
length : int > 0, optional
If provided, the output ``y`` is zero-padded or clipped to exactly
``length`` samples.
res_type : string
Resampling mode.
See `librosa.resample` for supported modes.
dtype : numeric type
Real numeric type for ``y``. Default is inferred to match the numerical
precision of the input CQT.
Returns
-------
y : np.ndarray, [shape=(..., n_samples), dtype=np.float]
Audio time-series reconstructed from the CQT representation.
See Also
--------
cqt
librosa.resample
Notes
-----
This function caches at level 40.
Examples
--------
Using default parameters
>>> y, sr = librosa.load(librosa.ex('trumpet'))
>>> C = librosa.cqt(y=y, sr=sr)
>>> y_hat = librosa.icqt(C=C, sr=sr)
Or with a different hop length and frequency resolution:
>>> hop_length = 256
>>> bins_per_octave = 12 * 3
>>> C = librosa.cqt(y=y, sr=sr, hop_length=256, n_bins=7*bins_per_octave,
... bins_per_octave=bins_per_octave)
>>> y_hat = librosa.icqt(C=C, sr=sr, hop_length=hop_length,
... bins_per_octave=bins_per_octave)
"""
if fmin is None:
fmin = note_to_hz("C1")
# Apply tuning correction
fmin = fmin * 2.0 ** (tuning / bins_per_octave)
# Get the top octave of frequencies
n_bins = C.shape[-2]
n_octaves = int(np.ceil(float(n_bins) / bins_per_octave))
# truncate the cqt to max frames if helpful
freqs = cqt_frequencies(fmin=fmin, n_bins=n_bins, bins_per_octave=bins_per_octave)
alpha = __bpo_to_alpha(bins_per_octave)
lengths, f_cutoff = filters.wavelet_lengths(
freqs=freqs, sr=sr, window=window, filter_scale=filter_scale, alpha=alpha
)
# Trim the CQT to only what's necessary for reconstruction
if length is not None:
n_frames = int(np.ceil((length + max(lengths)) / hop_length))
C = C[..., :n_frames]
C_scale = np.sqrt(lengths)
# This shape array will be used for broadcasting the basis scale
# we'll have to adapt this per octave within the loop
y: Optional[np.ndarray] = None
# Assume the top octave is at the full rate
srs = [sr]
hops = [hop_length]
for i in range(n_octaves - 1):
if hops[0] % 2 == 0:
# We can downsample:
srs.insert(0, srs[0] * 0.5)
hops.insert(0, hops[0] // 2)
else:
# We're out of downsamplings, carry forward
srs.insert(0, srs[0])
hops.insert(0, hops[0])
for i, (my_sr, my_hop) in enumerate(zip(srs, hops)):
# How many filters are in this octave?
n_filters = min(bins_per_octave, n_bins - bins_per_octave * i)
# Slice out the current octave
sl = slice(bins_per_octave * i, bins_per_octave * i + n_filters)
fft_basis, n_fft, _ = __vqt_filter_fft(
my_sr,
freqs[sl],
filter_scale,
norm,
sparsity,
window=window,
dtype=dtype,
alpha=alpha,
)
# Transpose the basis
inv_basis = fft_basis.H.todense()
# Compute each filter's frequency-domain power
freq_power = 1 / np.sum(util.abs2(np.asarray(inv_basis)), axis=0)
# Compensate for length normalization in the forward transform
freq_power *= n_fft / lengths[sl]
# Inverse-project the basis for each octave
if scale:
# scale=True ==> re-scale by sqrt(lengths)
D_oct = np.einsum(
"fc,c,c,...ct->...ft",
inv_basis,
C_scale[sl],
freq_power,
C[..., sl, :],
optimize=True,
)
else:
D_oct = np.einsum(
"fc,c,...ct->...ft", inv_basis, freq_power, C[..., sl, :], optimize=True
)
y_oct = istft(D_oct, window="ones", hop_length=my_hop, dtype=dtype)
y_oct = audio.resample(
y_oct,
orig_sr=1,
target_sr=sr // my_sr,
res_type=res_type,
scale=False,
fix=False,
)
if y is None:
y = y_oct
else:
y[..., : y_oct.shape[-1]] += y_oct
# make mypy happy
assert y is not None
if length:
y = util.fix_length(y, size=length)
return y
@cache(level=20)
def vqt(
y: np.ndarray,
*,
sr: float = 22050,
hop_length: int = 512,
fmin: Optional[_FloatLike_co] = None,
n_bins: int = 84,
intervals: Union[str, Collection[float]] = "equal",
gamma: Optional[float] = None,
bins_per_octave: int = 12,
tuning: Optional[float] = 0.0,
filter_scale: float = 1,
norm: Optional[float] = 1,
sparsity: float = 0.01,
window: _WindowSpec = "hann",
scale: bool = True,
pad_mode: _PadMode = "constant",
res_type: Optional[str] = "soxr_hq",
dtype: Optional[DTypeLike] = None,
) -> np.ndarray:
"""Compute the variable-Q transform of an audio signal.
This implementation is based on the recursive sub-sampling method
described by [#]_.
.. [#] Schörkhuber, Christian, Anssi Klapuri, Nicki Holighaus, and Monika Dörfler.
"A Matlab toolbox for efficient perfect reconstruction time-frequency
transforms with log-frequency resolution."
In Audio Engineering Society Conference: 53rd International Conference: Semantic Audio.
Audio Engineering Society, 2014.
Parameters
----------
y : np.ndarray [shape=(..., n)]
audio time series. Multi-channel is supported.
sr : number > 0 [scalar]
sampling rate of ``y``
hop_length : int > 0 [scalar]
number of samples between successive VQT columns.
fmin : float > 0 [scalar]
Minimum frequency. Defaults to `C1 ~= 32.70 Hz`
n_bins : int > 0 [scalar]
Number of frequency bins, starting at ``fmin``
intervals : str or array of floats in [1, 2)
Either a string specification for an interval set, e.g.,
`'equal'`, `'pythagorean'`, `'ji3'`, etc. or an array of
intervals expressed as numbers between 1 and 2.
.. see also:: librosa.interval_frequencies
gamma : number > 0 [scalar]
Bandwidth offset for determining filter lengths.
If ``gamma=0``, produces the constant-Q transform.
If 'gamma=None', gamma will be calculated such that filter bandwidths are equal to a
constant fraction of the equivalent rectangular bandwidths (ERB). This is accomplished
by solving for the gamma which gives::
B_k = alpha * f_k + gamma = C * ERB(f_k),
where ``B_k`` is the bandwidth of filter ``k`` with center frequency ``f_k``, alpha
is the inverse of what would be the constant Q-factor, and ``C = alpha / 0.108`` is the
constant fraction across all filters.
Here we use ``ERB(f_k) = 24.7 + 0.108 * f_k``, the best-fit curve derived
from experimental data in [#]_.
.. [#] Glasberg, Brian R., and Brian CJ Moore.
"Derivation of auditory filter shapes from notched-noise data."
Hearing research 47.1-2 (1990): 103-138.
bins_per_octave : int > 0 [scalar]
Number of bins per octave
tuning : None or float
Tuning offset in fractions of a bin.
If ``None``, tuning will be automatically estimated from the signal.
The minimum frequency of the resulting VQT will be modified to
``fmin * 2**(tuning / bins_per_octave)``.
filter_scale : float > 0
Filter scale factor. Small values (<1) use shorter windows
for improved time resolution.
norm : {inf, -inf, 0, float > 0}
Type of norm to use for basis function normalization.
See `librosa.util.normalize`.
sparsity : float in [0, 1)
Sparsify the VQT basis by discarding up to ``sparsity``
fraction of the energy in each basis.
Set ``sparsity=0`` to disable sparsification.
window : str, tuple, number, or function
Window specification for the basis filters.
See `filters.get_window` for details.
scale : bool
If ``True``, scale the VQT response by square-root the length of
each channel's filter. This is analogous to ``norm='ortho'`` in FFT.
If ``False``, do not scale the VQT. This is analogous to
``norm=None`` in FFT.
pad_mode : string
Padding mode for centered frame analysis.
See also: `librosa.stft` and `numpy.pad`.
res_type : string
The resampling mode for recursive downsampling.
dtype : np.dtype
The dtype of the output array. By default, this is inferred to match the
numerical precision of the input signal.
Returns
-------
VQT : np.ndarray [shape=(..., n_bins, t), dtype=np.complex]
Variable-Q value each frequency at each time.
See Also
--------
cqt
Notes
-----
This function caches at level 20.
Examples
--------
Generate and plot a variable-Q power spectrum
>>> import matplotlib.pyplot as plt
>>> y, sr = librosa.load(librosa.ex('choice'), duration=5)
>>> C = np.abs(librosa.cqt(y, sr=sr))
>>> V = np.abs(librosa.vqt(y, sr=sr))
>>> fig, ax = plt.subplots(nrows=2, sharex=True, sharey=True)
>>> librosa.display.specshow(librosa.amplitude_to_db(C, ref=np.max),
... sr=sr, x_axis='time', y_axis='cqt_note', ax=ax[0])
>>> ax[0].set(title='Constant-Q power spectrum', xlabel=None)
>>> ax[0].label_outer()
>>> img = librosa.display.specshow(librosa.amplitude_to_db(V, ref=np.max),
... sr=sr, x_axis='time', y_axis='cqt_note', ax=ax[1])
>>> ax[1].set_title('Variable-Q power spectrum')
>>> fig.colorbar(img, ax=ax, format="%+2.0f dB")
"""
# If intervals are provided as an array, override BPO
if not isinstance(intervals, str):
bins_per_octave = len(intervals)
# How many octaves are we dealing with?
n_octaves = int(np.ceil(float(n_bins) / bins_per_octave))
n_filters = min(bins_per_octave, n_bins)
if fmin is None:
# C1 by default
fmin = note_to_hz("C1")
if tuning is None:
tuning = estimate_tuning(y=y, sr=sr, bins_per_octave=bins_per_octave)
if dtype is None:
dtype = util.dtype_r2c(y.dtype)
# Apply tuning correction
fmin = fmin * 2.0 ** (tuning / bins_per_octave)
# First thing, get the freqs of the top octave
freqs = interval_frequencies(
n_bins=n_bins,
fmin=fmin,
intervals=intervals,
bins_per_octave=bins_per_octave,
sort=True,
)
freqs_top = freqs[-bins_per_octave:]
fmax_t: float = np.max(freqs_top)
alpha = __bpo_to_alpha(bins_per_octave)
lengths, filter_cutoff = filters.wavelet_lengths(
freqs=freqs,
sr=sr,
window=window,
filter_scale=filter_scale,
gamma=gamma,
alpha=alpha,
)
# Determine required resampling quality
nyquist = sr / 2.0
if filter_cutoff > nyquist:
raise ParameterError(
f"Wavelet basis with max frequency={fmax_t} would exceed the Nyquist frequency={nyquist}. "
"Try reducing the number of frequency bins."
)
if res_type is None:
warnings.warn(
"Support for VQT with res_type=None is deprecated in librosa 0.10\n"
"and will be removed in version 1.0.",
category=FutureWarning,
stacklevel=2,
)
res_type = "soxr_hq"
y, sr, hop_length = __early_downsample(
y, sr, hop_length, res_type, n_octaves, nyquist, filter_cutoff, scale
)
vqt_resp = []
# Iterate down the octaves
my_y, my_sr, my_hop = y, sr, hop_length
for i in range(n_octaves):
# Slice out the current octave of filters
if i == 0:
sl = slice(-n_filters, None)
else:
sl = slice(-n_filters * (i + 1), -n_filters * i)
# This may be incorrect with early downsampling
freqs_oct = freqs[sl]
fft_basis, n_fft, _ = __vqt_filter_fft(
my_sr,
freqs_oct,
filter_scale,
norm,
sparsity,
window=window,
gamma=gamma,
dtype=dtype,
alpha=alpha,
)
# Re-scale the filters to compensate for downsampling
fft_basis[:] *= np.sqrt(sr / my_sr)
# Compute the vqt filter response and append to the stack
vqt_resp.append(
__cqt_response(my_y, n_fft, my_hop, fft_basis, pad_mode, dtype=dtype)
)
if my_hop % 2 == 0:
my_hop //= 2
my_sr /= 2.0
my_y = audio.resample(
my_y, orig_sr=2, target_sr=1, res_type=res_type, scale=True
)
V = __trim_stack(vqt_resp, n_bins, dtype)
if scale:
# Recompute lengths here because early downsampling may have changed
# our sampling rate
lengths, _ = filters.wavelet_lengths(
freqs=freqs,
sr=sr,
window=window,
filter_scale=filter_scale,
gamma=gamma,
alpha=alpha,
)
# reshape lengths to match V shape
lengths = util.expand_to(lengths, ndim=V.ndim, axes=-2)
V /= np.sqrt(lengths)
return V
@cache(level=10)
def __vqt_filter_fft(
sr,
freqs,
filter_scale,
norm,
sparsity,
hop_length=None,
window="hann",
gamma=0.0,
dtype=np.complex64,
alpha=None,
):
"""Generate the frequency domain variable-Q filter basis."""
basis, lengths = filters.wavelet(
freqs=freqs,
sr=sr,
filter_scale=filter_scale,
norm=norm,
pad_fft=True,
window=window,
gamma=gamma,
alpha=alpha,
)
# Filters are padded up to the nearest integral power of 2
n_fft = basis.shape[1]
if hop_length is not None and n_fft < 2.0 ** (1 + np.ceil(np.log2(hop_length))):
n_fft = int(2.0 ** (1 + np.ceil(np.log2(hop_length))))
# re-normalize bases with respect to the FFT window length
basis *= lengths[:, np.newaxis] / float(n_fft)
# FFT and retain only the non-negative frequencies
fft = get_fftlib()
fft_basis = fft.fft(basis, n=n_fft, axis=1)[:, : (n_fft // 2) + 1]
# sparsify the basis
fft_basis = util.sparsify_rows(fft_basis, quantile=sparsity, dtype=dtype)
return fft_basis, n_fft, lengths
def __trim_stack(
cqt_resp: List[np.ndarray], n_bins: int, dtype: DTypeLike
) -> np.ndarray:
"""Helper function to trim and stack a collection of CQT responses"""
max_col = min(c_i.shape[-1] for c_i in cqt_resp)
# Grab any leading dimensions
shape = list(cqt_resp[0].shape)
shape[-2] = n_bins
shape[-1] = max_col
cqt_out = np.empty(shape, dtype=dtype, order="F")
# Copy per-octave data into output array
end = n_bins
for c_i in cqt_resp:
# By default, take the whole octave
n_oct = c_i.shape[-2]
# If the whole octave is more than we can fit,
# take the highest bins from c_i
if end < n_oct:
cqt_out[..., :end, :] = c_i[..., -end:, :max_col]
else:
cqt_out[..., end - n_oct : end, :] = c_i[..., :max_col]
end -= n_oct
return cqt_out
def __cqt_response(
y, n_fft, hop_length, fft_basis, mode, window="ones", phase=True, dtype=None
):
"""Compute the filter response with a target STFT hop."""
# Compute the STFT matrix
D = stft(
y, n_fft=n_fft, hop_length=hop_length, window=window, pad_mode=mode, dtype=dtype
)
if not phase:
D = np.abs(D)
# Reshape D to Dr
Dr = D.reshape((-1, D.shape[-2], D.shape[-1]))
output_flat = np.empty(
(Dr.shape[0], fft_basis.shape[0], Dr.shape[-1]), dtype=D.dtype
)
# iterate over channels
# project fft_basis.dot(Dr[i])
for i in range(Dr.shape[0]):
output_flat[i] = fft_basis.dot(Dr[i])
# reshape Dr to match D's leading dimensions again
shape = list(D.shape)
shape[-2] = fft_basis.shape[0]
return output_flat.reshape(shape)
def __early_downsample_count(nyquist, filter_cutoff, hop_length, n_octaves):
"""Compute the number of early downsampling operations"""
downsample_count1 = max(0, int(np.ceil(np.log2(nyquist / filter_cutoff)) - 1) - 1)
num_twos = __num_two_factors(hop_length)
downsample_count2 = max(0, num_twos - n_octaves + 1)
return min(downsample_count1, downsample_count2)
def __early_downsample(
y, sr, hop_length, res_type, n_octaves, nyquist, filter_cutoff, scale
):
"""Perform early downsampling on an audio signal, if it applies."""
downsample_count = __early_downsample_count(
nyquist, filter_cutoff, hop_length, n_octaves
)
if downsample_count > 0:
downsample_factor = 2 ** (downsample_count)
hop_length //= downsample_factor
if y.shape[-1] < downsample_factor:
raise ParameterError(
f"Input signal length={len(y):d} is too short for "
f"{n_octaves:d}-octave CQT"
)
new_sr = sr / float(downsample_factor)
y = audio.resample(
y, orig_sr=downsample_factor, target_sr=1, res_type=res_type, scale=True
)
# If we're not going to length-scale after CQT, we
# need to compensate for the downsampling factor here
if not scale:
y *= np.sqrt(downsample_factor)
sr = new_sr
return y, sr, hop_length
@jit(nopython=True, cache=False)
def __num_two_factors(x):
"""Return how many times integer x can be evenly divided by 2.
Returns 0 for non-positive integers.
"""
if x <= 0:
return 0
num_twos = 0
while x % 2 == 0:
num_twos += 1
x //= 2
return num_twos
def griffinlim_cqt(
C: np.ndarray,
*,
n_iter: int = 32,
sr: float = 22050,
hop_length: int = 512,
fmin: Optional[_FloatLike_co] = None,
bins_per_octave: int = 12,
tuning: float = 0.0,
filter_scale: float = 1,
norm: Optional[float] = 1,
sparsity: float = 0.01,
window: _WindowSpec = "hann",
scale: bool = True,
pad_mode: _PadMode = "constant",
res_type: str = "soxr_hq",
dtype: Optional[DTypeLike] = None,
length: Optional[int] = None,
momentum: float = 0.99,
init: Optional[str] = "random",
random_state: Optional[
Union[int, np.random.RandomState, np.random.Generator]
] = None,
) -> np.ndarray:
"""Approximate constant-Q magnitude spectrogram inversion using the "fast" Griffin-Lim
algorithm.
Given the magnitude of a constant-Q spectrogram (``C``), the algorithm randomly initializes
phase estimates, and then alternates forward- and inverse-CQT operations. [#]_
This implementation is based on the (fast) Griffin-Lim method for Short-time Fourier Transforms, [#]_
but adapted for use with constant-Q spectrograms.
.. [#] D. W. Griffin and J. S. Lim,
"Signal estimation from modified short-time Fourier transform,"
IEEE Trans. ASSP, vol.32, no.2, pp.236–243, Apr. 1984.
.. [#] Perraudin, N., Balazs, P., & Søndergaard, P. L.
"A fast Griffin-Lim algorithm,"
IEEE Workshop on Applications of Signal Processing to Audio and Acoustics (pp. 1-4),
Oct. 2013.
Parameters
----------
C : np.ndarray [shape=(..., n_bins, n_frames)]
The constant-Q magnitude spectrogram
n_iter : int > 0
The number of iterations to run
sr : number > 0
Audio sampling rate
hop_length : int > 0
The hop length of the CQT
fmin : number > 0
Minimum frequency for the CQT.
If not provided, it defaults to `C1`.
bins_per_octave : int > 0
Number of bins per octave
tuning : float
Tuning deviation from A440, in fractions of a bin
filter_scale : float > 0
Filter scale factor. Small values (<1) use shorter windows
for improved time resolution.
norm : {inf, -inf, 0, float > 0}
Type of norm to use for basis function normalization.
See `librosa.util.normalize`.
sparsity : float in [0, 1)
Sparsify the CQT basis by discarding up to ``sparsity``
fraction of the energy in each basis.
Set ``sparsity=0`` to disable sparsification.
window : str, tuple, or function
Window specification for the basis filters.
See `filters.get_window` for details.
scale : bool
If ``True``, scale the CQT response by square-root the length
of each channel's filter. This is analogous to ``norm='ortho'``
in FFT.
If ``False``, do not scale the CQT. This is analogous to ``norm=None``
in FFT.
pad_mode : string
Padding mode for centered frame analysis.
See also: `librosa.stft` and `numpy.pad`.
res_type : string
The resampling mode for recursive downsampling.
See ``librosa.resample`` for a list of available options.
dtype : numeric type
Real numeric type for ``y``. Default is inferred to match the precision
of the input CQT.
length : int > 0, optional
If provided, the output ``y`` is zero-padded or clipped to exactly
``length`` samples.
momentum : float > 0
The momentum parameter for fast Griffin-Lim.
Setting this to 0 recovers the original Griffin-Lim method.
Values near 1 can lead to faster convergence, but above 1 may not converge.
init : None or 'random' [default]
If 'random' (the default), then phase values are initialized randomly
according to ``random_state``. This is recommended when the input ``C`` is
a magnitude spectrogram with no initial phase estimates.
If ``None``, then the phase is initialized from ``C``. This is useful when
an initial guess for phase can be provided, or when you want to resume
Griffin-Lim from a previous output.
random_state : None, int, np.random.RandomState, or np.random.Generator
If int, random_state is the seed used by the random number generator
for phase initialization.
If `np.random.RandomState` or `np.random.Generator` instance, the random number generator itself.
If ``None``, defaults to the `np.random.default_rng()` object.
Returns
-------
y : np.ndarray [shape=(..., n)]
time-domain signal reconstructed from ``C``
See Also
--------
cqt
icqt
griffinlim
filters.get_window
resample
Examples
--------
A basis CQT inverse example
>>> y, sr = librosa.load(librosa.ex('trumpet', hq=True), sr=None)
>>> # Get the CQT magnitude, 7 octaves at 36 bins per octave
>>> C = np.abs(librosa.cqt(y=y, sr=sr, bins_per_octave=36, n_bins=7*36))
>>> # Invert using Griffin-Lim
>>> y_inv = librosa.griffinlim_cqt(C, sr=sr, bins_per_octave=36)
>>> # And invert without estimating phase
>>> y_icqt = librosa.icqt(C, sr=sr, bins_per_octave=36)
Wave-plot the results
>>> import matplotlib.pyplot as plt
>>> fig, ax = plt.subplots(nrows=3, sharex=True, sharey=True)
>>> librosa.display.waveshow(y, sr=sr, color='b', ax=ax[0])
>>> ax[0].set(title='Original', xlabel=None)
>>> ax[0].label_outer()
>>> librosa.display.waveshow(y_inv, sr=sr, color='g', ax=ax[1])
>>> ax[1].set(title='Griffin-Lim reconstruction', xlabel=None)
>>> ax[1].label_outer()
>>> librosa.display.waveshow(y_icqt, sr=sr, color='r', ax=ax[2])
>>> ax[2].set(title='Magnitude-only icqt reconstruction')
"""
if fmin is None:
fmin = note_to_hz("C1")
if random_state is None:
rng = np.random.default_rng()
elif isinstance(random_state, int):
rng = np.random.RandomState(seed=random_state) # type: ignore
elif isinstance(random_state, (np.random.RandomState, np.random.Generator)):
rng = random_state # type: ignore
else:
_ensure_not_reachable(random_state)
raise ParameterError(f"Unsupported random_state={random_state!r}")
if momentum > 1:
warnings.warn(
f"Griffin-Lim with momentum={momentum} > 1 can be unstable. "
"Proceed with caution!",
stacklevel=2,
)
elif momentum < 0:
raise ParameterError(f"griffinlim_cqt() called with momentum={momentum} < 0")
# using complex64 will keep the result to minimal necessary precision
angles = np.empty(C.shape, dtype=np.complex64)
eps = util.tiny(angles)
if init == "random":
# randomly initialize the phase
angles[:] = util.phasor(2 * np.pi * rng.random(size=C.shape))
elif init is None:
# Initialize an all ones complex matrix
angles[:] = 1.0
else:
raise ParameterError(f"init={init} must either None or 'random'")
# And initialize the previous iterate to 0
rebuilt: np.ndarray = np.array(0.0)
for _ in range(n_iter):
# Store the previous iterate
tprev = rebuilt
# Invert with our current estimate of the phases
inverse = icqt(
C * angles,
sr=sr,
hop_length=hop_length,
bins_per_octave=bins_per_octave,
fmin=fmin,
tuning=tuning,
filter_scale=filter_scale,
window=window,
length=length,
res_type=res_type,
norm=norm,
scale=scale,
sparsity=sparsity,
dtype=dtype,
)
# Rebuild the spectrogram
rebuilt = cqt(
inverse,
sr=sr,
bins_per_octave=bins_per_octave,
n_bins=C.shape[-2],
hop_length=hop_length,
fmin=fmin,
tuning=tuning,
filter_scale=filter_scale,
window=window,
norm=norm,
scale=scale,
sparsity=sparsity,
pad_mode=pad_mode,
res_type=res_type,
)
# Update our phase estimates
angles[:] = rebuilt - (momentum / (1 + momentum)) * tprev
angles[:] /= np.abs(angles) + eps
# Return the final phase estimates
return icqt(
C * angles,
sr=sr,
hop_length=hop_length,
bins_per_octave=bins_per_octave,
tuning=tuning,
filter_scale=filter_scale,
fmin=fmin,
window=window,
length=length,
res_type=res_type,
norm=norm,
scale=scale,
sparsity=sparsity,
dtype=dtype,
)
def __bpo_to_alpha(bins_per_octave: int) -> float:
"""Compute the alpha coefficient for a given number of bins per octave
Parameters
----------
bins_per_octave : int
Returns
-------
alpha : number > 0
"""
r = 2 ** (1 / bins_per_octave)
return (r**2 - 1) / (r**2 + 1)
|