Every content here is my original work.Creative Commons Licence
Universitas Scripta is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Your cookies may be used by Google and mathjax. See Google's privacy policy.
Showing posts with label Audio. Show all posts
Showing posts with label Audio. Show all posts

Saturday, 18 May 2024

Interpolation function training

Ideal interpolation kernel is sinc function, which consists of all frequencies upto Nyquist frequency. However, this involves infinite-dimension convolution in discrete calculation, so we apply window function. In signal procession, thus, interpolation kernel is windowed-sinc function [https://www.analog.com/media/en/technical-documentation/dsp-book/dsp_book_Ch16.pdf], and the window function can vary to achieve (1) to narrow the window width to reduce calculation, (2) and in the same time to increase accuracy and reduce artifacts. We have to satisfy these two contradictory conditions, so we have to compromise and optimise conditions.

    The kernel generally must satisfy f(0) = 1 and f(n != 0 but integer) = 0 and additionally I force to satisfy to be continuous function, to avoid catastrophic artifacts. Generic window function may be written in Fourier series, (cosine if symmetric) [https://en.wikipedia.org/wiki/Window_function] 

\[ \displaystyle
w(t)=
\begin{cases}
 \sum_{k=0}^{N}a_k \cos k \pi t  + \sum_{k=1}^{N} b_k \sin k\pi t\quad\text{if  \(t\in[-1,1]\)}
\\
0\quad\text{else}
\end{cases} \]

with absolute constraints

\[\textbf{Constraint 1: }\quad w(0) = \sum_{k=0}^{N} a_k = 1,\]

\[\textbf{Constraint 2: }\quad w(1) =\sum_{k=0}^{N}(-1)^k a_k = 0, \]

assuming the window width to be [-1, 1] (to use in practice, scale this in \(t\)-direction) .


Projection of the window parameters \(\{a_k, b_k\}\) to the constraints.

    From \(\{a_k, b_k\}\) vector space, we have two constraints to absolutely be satisfied. Analogously to this situation: we must project a point to two planes simultaneosly, thus making it on the line, the intersection of the two planes; The projection must move perperdicular to either plane. We can develop a training process only moving on the constraints, but because of calculation errors, we have to develop a way to project on the constraints anyway. The training process to optimise the kernel to the given big data of audio may correspond to the loss optimization process of this \((2N+1)\)-dimensional point moving to the minima or maxima.

    Let us concretise the projection idea more. The \((2N+1)\)-dimensional point always satisfy 2 constraints above, so the valid space satisfying the constraints is \((2N-1)\) dimensions. In the quotient space, the kernel corresponding to a certain point must be \(2\)-dimensional, which passes the given point and is parallel to the two perpendicular vectors of the two constraint \(2N\)-plane. The intersection between the proper \((2N-1)\) dimensions and the 2D-kernel is the projected point we want.

    The two perpendicular vectors is the same as the coefficients of the equations of the planes, so the 2D kernel including a point \(\{a'_k, b'_k\}\) is written by 

\[ \{a_k, b_k\}  = \{a'_k, b'_k\} + \{1, 0\} x + \{(-1)^k, 0\} y \]

To obtain the intersection of this kernel to the two constraints \(w(0), w(1)\), we may substitute the  \(\{a_k, b_k\}\) expression of the kernel to the equation of each constraint.

\[ w(0) = \sum_{k=0}^{N} (a'_k + x + (-1)^k y) = 1 \]

\[ w(1) = \sum_{k=0}^{N} (-1)^k(a'_k + x + (-1)^k y) = 0 \]

From these equations, we have to obtain \(x\) and \(y\) to obtain how much the intersection have moved inside the kernel space from the original point. Simplified,

\[ w(0) =(N+1) x + \frac{1 + (-1)^N}{2}y + \sum_{k=0}^{N} a'_k  = 1 \]

\[ w(1) = \frac{1 + (-1)^N}{2} x + (N+1)y + \sum_{k=0}^{N} (-1)^k a'_k = 0 \]

More simplified,

\[
\begin{pmatrix}
(N+1) & \frac{1 + (-1)^N}{2}
\\ \frac{1 + (-1)^N}{2} & (N+1)
\end{pmatrix}
\begin{pmatrix} x\\y \end{pmatrix}
=
\begin{pmatrix}
1-\sum_{k=0}^{N} a'_k 
\\
-\sum_{k=0}^{N} (-1)^k a'_k 
\end{pmatrix}
\]

\[
\Rightarrow
\begin{pmatrix} x\\y \end{pmatrix}
=
\frac{1}{(N+1)^2 - \frac{1 + (-1)^N}{2}}
\begin{pmatrix}
(N+1) & -\frac{1 + (-1)^N}{2}
\\ -\frac{1 + (-1)^N}{2} & (N+1)
\end{pmatrix}
\begin{pmatrix}
1-\sum_{k=0}^{N} a'_k 
\\
-\sum_{k=0}^{N} (-1)^k a'_k 
\end{pmatrix}
\]

\[\Rightarrow
\begin{pmatrix} x+y\\x-y \end{pmatrix}=
\dfrac{
  (N+1) -\frac{1+(-1)^N}{2} \mathrm{diag}\{1, -1\}
}{(N+1)^2 - \frac{1+(-1)^N}{2}}
\begin{pmatrix}
1-\sum_{k=0}^{N} a'_k -\sum_{k=0}^{N} (-1)^k a'_k 
\\
1-\sum_{k=0}^{N} a'_k +\sum_{k=0}^{N} (-1)^k a'_k 
\end{pmatrix}
\]

\[\Rightarrow
\begin{pmatrix} x+y\\x-y \end{pmatrix}=
\dfrac{
  (N+1) -\frac{1+(-1)^N}{2} \mathrm{diag}\{1, -1\}
}{(N+1)^2 - \frac{1+(-1)^N}{2}}
\begin{pmatrix}
1-2\sum_{k=0}^{N} \frac{1 + (-1)^k}{2} a'_k 
\\
1-2\sum_{k=0}^{N} \frac{1 + (-1)^{k+1}}{2} a'_k 
\end{pmatrix}
\]

Thus, the projected point \(\{a_k, b_k\}\) from the original point \(\{a'_k, b'_k\}\) is

\[
\begin{cases}
a_k = a'_k + x + (-1)^k y,\quad k\in[0,N],
\\
b_k = b'_k,\quad k\in[1,N].
\end{cases}
\]

\[
\displaystyle
\Rightarrow
\begin{cases}
a_k = a'_k +
\dfrac{
  (N+1) -\frac{1+(-1)^N}{2} (-1)^k
}{(N+1)^2 - \frac{1+(-1)^N}{2}}
\left(
1 -2\sum_{m=0}^{N}\frac{1+ (-1)^{k+m}}{2} a'_m 
\right)
\\
b_k = b'_k,\quad k\in[1,N].
\end{cases}
\]

    Thus, here we may define projection operator \(P\) s.t.

\[
\displaystyle
\begin{cases}
P(a_k) := a_k +
\dfrac{
  (N+1) -\frac{1+(-1)^N}{2} (-1)^k
}{(N+1)^2 - \frac{1+(-1)^N}{2}}
\left(
1 -2\sum_{m=0}^{N}\frac{1+ (-1)^{k+m}}{2} a_m 
\right)
\\
P(b_k) := b_k,\quad k\in[1,N].
\end{cases}
\]


Gradient vector projection to the constraints

Now, as doing projection each iteration of training is inefficient, we want to develop to project each step only on the constraints.

    From the original valid point \(\{a^0_k, b^0_k\}\) s.t. \(P(a^0_k\) = a^0_k, we want to consider new train step \(\{a^0_k + \Delta a_k, b^0_k + \Delta b_k\}\) where \(\Delta a_k, \Delta b_k\) may not be determined with constraints but only gradient of loss. Then, we must project \(P(a^0_k + \Delta a_k)\) to finalize the step.

\[
\small
\begin{aligned}
P(a^0_k+\Delta a_k) &= 
a^0_k +\Delta a_k+
\dfrac{
  (N+1) -\frac{1+(-1)^N}{2} (-1)^k
}{(N+1)^2 - \frac{1+(-1)^N}{2}}
\left(
1 -2\sum_{m=0}^{N}\frac{1+ (-1)^{k+m}}{2} (a^0_m + \Delta a_m)
\right)
\\ &= P(a^0_k) + \Delta a_k
-2
\dfrac{
  (N+1) -\frac{1+(-1)^N}{2} (-1)^k
}{(N+1)^2 - \frac{1+(-1)^N}{2}}
\sum_{m=0}^{N}\frac{1+ (-1)^{k+m}}{2} \Delta a_m
\end{aligned}
\]

    Thus, here we define the new projection operator \(\Delta P\) for the differentiation vector of the coefficient point:

\[
\Delta P(\Delta a_k) :=  \Delta a_k
-2
\dfrac{
  (N+1) -\frac{1+(-1)^N}{2} (-1)^k
}{(N+1)^2 - \frac{1+(-1)^N}{2}}
\sum_{m=0}^{N}\frac{1+ (-1)^{k+m}}{2} \Delta a_m
\]


Pseudocodes

\(P\) and \(\Delta P\) can be written in PyTorch codes:

Friday, 2 September 2022

Lightning to usb3 camera adapter review

 시중에 이미 많은 usb 킷들이 나와있지만 그래도 애플 정품은 전원부가 다를까 싶어서 구입해보았다. 예상 외의 부분들이 있었다.

우선 일반적인 써드파티 꼬다리보다는 확실히 전원부 품질은 좋았다. 중국산 lightning male to usb c female을 써서 dac를 연결했을때는 노이즈가 심해서 원래 dac품질의 문제로 생각했었다. 그런데 애플정품 젠더를 이용하니 노이즈 문제는 확실히 감소했다. 외부 전원 공급으로 인한 품질 향상을 기대했으나 폰 자체 전원만으로도 이런 차이가 있었다.

문제는 외부전원에 있었다. 외부전원공급시에 내가 기대한것은 전원 품질 향상과 용량확충이었다. 둘 다 문제가 있었다. 우선 외부전원을 연결하고 dac를 연결하니 한쪽 귀에서 노이즈가 들리고 전기가 튀기 시작했다. 아뿔싸. 역시 그라운드 루프 문제는 해결하지 못했구나... 특수장치를 쓰지 않는 이상 일반적인 경우는 다들 마찬가지일 것이다. 충전기에서는 flyback converter를 통해 허수전압이 연결된 chassis ground를 쓸 것이고, 이 전압이 폰 내부 gnd랑 달라서 전기가 흐른다. 도저히 귀가 아파서 쓸수가 없었다.

dac꼬다리 뿐 아니라, usb용 소형 거치형 dac도 연결해 보았으나, 전원이 아예 켜지지 않았다. 이것도 예상외였다. 보통 usb1은 100mA, usb2는 500mA, usb 3은 900mA의 전류공급을 보장한다. 하지만 lightning에서 audio accessory로 출력할수 있는 최대전류는 50mA, 순간최대 100mA로 규정되어 있으므로 (애플 lightning accessory guideline참조) 전류가 턱없이 부족하다. usb3 kit를 산 이유도 이런 전류 부족을 해결하여 외부기기도 사용할수있게 하기 위함도 있었는데, usb기기의 전원도 안켜진다는것은 아마 십중팔구 전류부족일것이다.

역시 외부 ssd도 연결이 불가능했다.

따라서 다른분들에게는 lightning to usb2 kit을 추천한다. 이미 대부분의 기기들이 이것을 기준으로 만들어져있어서 연결성도 좋다. usb3 kit은 전원연결부 때문에 usb-a-male port에 연결할수도 없다. (ifi portable dac 같은 제품들) 

애플 자체 펌웨어 업데이트가 지원된다는것은 좋지만, 실질적인 효용성이 뭔지는 모르겠다.

미안하지만, 태클은 사절이다. 당신 분야에 대해서 당신이 아는것과 다른 단어를 써서 화가 날수도 있겠지만, 나는 아는 것이 많아서 뭉뚱그려서 두리뭉실하게 말하는거니까. 별로 틀린말 하는것도 아니고 당신 전문분야의 용어를 안쓴다고 뭐라고 하지는 말기를

Wednesday, 3 August 2022

Solution to the high frequency damping problem by low-pass filter

Previously, I showed you how the simplest R2R DAC-generating step-functioned signals is damped by the low-pass filter from the original ideal signal, and this is fundamental not artificial, so no one can regenerate the original signal.

Solution to this is, of course, studied by many people. As I hinted in the previous article, I thought that to interpolate and to regenerate the original signal, I may have to do Fourier transform and then inverse Fourier transform. I started to search in such direction. And then, I found a great article teaching the sinc interpolation.

http://www.ipol.im/pub/art/2011/g_lmii/revisions/2011-09-27/g_lmii.html

sinc function is a great "Fourier-styled smooth" impact function. Only t=0 node is excited and other nodes are zero, so if this function is digitally sampled, that would look like just a impulse. Also, this function has all the frequencies fully from 0 to the Nyquist frequency. This function definitely will connect the dots smoothly, fully mobilizing all the lower-than-Nyquist frequencies.

If you think that this is too brick wall -- see the DAC datasheet and you can see the brick wall oversampling style, and this rings too much -- , then we can do more smooth. For example, suppressing the ringing by the normal distribution, \(e^{-t^2/\sigma^2} \mathrm{sinc} \pi f_s t\), makes the frequency response more smooth.

Friday, 27 May 2022

Hi-res is absolute necessity

People tend to forget the audio is analogue. Some says that only 44kHz is necessary because of Nyquist theorem. However this only proves that there is one-to-one correspondence between the original discrete data set and its Discrete Fourier Transform only if we eliminate the higher frequencies in the inverse Fourier transform. This does not mean that the discretely sampled data guarantees to be recovered as its original analogue wave function by the conventional audio circuit with low-pass filter.

Simple thought experiment reveals the contradiction in the 44kHz-sufficiency believers. 22kHz wave is annihilated in the sampling by 90-deg phase shifts. 11kHz is also only guaranteed to be sampled at least 1/sqrt(2) by amplitude.

In this article, I simulated DAC generating step function signals followed by an analogue first-order low-pass filter with its cut-off at the Nyquist frequency. The plot shows the average energy of the regenerated output analogue signal compared to the original conceptual sine wave to be sampled.

As I argued before, and some of audiophiles believe, the 24/192 satisfies the 1dB distortion threshold. To satisfy 1% (0.08dB) distortion threshold, we need to go 24/768. Remember that the human hearing function is not that attenuated at the higher frequency.

We can display frequency response of the low-pass filter we applied layered over the plot as orange. However, be careful that you cannot divide the attenuation into the contribution from the filter and the theoretical contribution. If you do a little thought experiment, you can easily realise that the theory already says that the maximum attenuation at fs/4 is -3dB, which is close to the total attenuation in the plot. The additional attenuation comes from the shape smoothing from the step-shaped original wave shape.






Tuesday, 31 August 2021

Self-research: Why hi-res sounds better? Suggestion to prove hi-res hearing theory

Some audiophiles and companies like Sony argue that high resolution audio matters while others argue that those are indistinguishable. These arguments can only be proven by statistics on blind tests. Some tests suggests that some people seems indeed distinguishes high resolution sources. Audio experts says that this is because the mixing and mastering target between CD sources and hi-res sources are different; they made to sound differently in the first place. I do personally believe the utility of the high resolution audio not only for mixing purpose but also for listening purpose. (For processing audio, hi-res definitely helps to reduce distortion. Digital filters is distorted heavily near the Nyquist frequency.) But I am not arguing that we can hear beyond 24kHz. ( Indeed some people can hear 24kHz but no beyond.) I am arguing that we need infinitely high digital sampling rate to reproduce 20Hz-24kHz analog signal.

  Think about 24kHz signal in 48kHz sampling. If the phase is shifted pi/2, the signal is shown as 0. The digital sampling with frequency in the last octave under the Nyquist frequency only has peak samples, so there is almost zero reproducibility concerning phase shift. Even if the frequency of the signal is not a divisor of the sampling frequency, ghost beating sound is produced. So, in CD Sound, 10-20kHz sound is not well reproduced and has no meaning unless it is harmonics of another lower sounds.

  2nd last octave sound under the Nyquist has a sample middle of the curve, so at least we can guarantee that 1/sqrt(2) of the amplitude of the original sound can be reproduced. (Or, maybe we can expect better by physical low-pass filter.) So on for the higher…

What I really recommend is to play 24/192 or at least 24/96 with 20khz lp filter.

So, I suggest an experiment to compare not only between different sources on the same player, but also between different lp filter but using the same hi-res source and the same DAC. If my theory is right, Sony’s hi-res capability issue of speakers are useless, but hi-res for DAC is important.

I have no professional background on this, so I dont know whether people already know this. I think my story makes sense physically, so most specialists maybe already know this.

But the reason I am raising this as problem is that I dont understand why Sony counts speaker’s reproducibility of 40khz signal. I know that there is meaning to have flatter FR in audible frequencies generally when we demand hi-res capability of analog device. But is there more meaning over that?

 http://archimago.blogspot.com/2016/07/musings-digital-interpolation-filters.html

https://en.wikipedia.org/wiki/Bilinear_transform

Thursday, 8 April 2021

IEC Standard Weightings

Transfer functions:

\(H_A(s)=\dfrac{(12194\,\mathrm{Hz})^2\:\left(\frac{s}{2\pi}\right)^4\:\big/\: e^{-2.0000/20}}{(\frac{s}{2\pi}+20.6\,\mathrm{Hz})^2\,(\frac{s}{2\pi}+107.7\,\mathrm{Hz})\,(\frac{s}{2\pi}+ 737.9\,\mathrm{Hz})\,(\frac{s}{2\pi}+12194\,\mathrm{Hz})^2}\)

\(H_B(s)=\dfrac{(12194\,\mathrm{Hz})^2\:\left(\frac{s}{2\pi}\right)^3\:\big/\:  e^{-0.1697/20}}{\left(\frac{s}{2\pi}+20.6\,\mathrm{Hz}\right)^2\:\left(\frac{s}{2\pi}+158.5\,\mathrm{Hz}\right)\:\left(\frac{s}{2\pi}+12194\,\mathrm{Hz}\right)^2}\)

\(H_C(s)=\dfrac{(12194\,\mathrm{Hz})^2\:\left(\frac{s}{2\pi}\right)^2\:\big/\:  e^{-0.0619/20}}{\left(\frac{s}{2\pi}+20.6\,\mathrm{Hz}\right)^2\:\left(\frac{s}{2\pi}+12194\,\mathrm{Hz}\right)^2}\)

\(H_Z(s)=\dfrac{(2\pi\cdot 46066\,\mathrm{Hz})^2\:s^2}{(s+2\pi\cdot 4.34\,\mathrm{Hz})^2\:(s+2\pi\cdot 46066\,\mathrm{Hz})^2}\)

\(H_K(s)=\dfrac{1.5848634463348217\,s^2+18886.914378028894\,s+1.1260607147003824\times10^8}{s^2+15004.846526655707\,s+1.1259450726979023\times10^8}\)

\(\times\dfrac{1.0049948987146884\,s^2}{s^2+478.91221140843305\, s+57414.259359025615}\times 10^{-0.691/20}\)

\(\times\dfrac{(2\pi\cdot 46066\,\mathrm{Hz})^2}{(s+2\pi\cdot 46066\,\mathrm{Hz})^2}\)

*only A,C,K are standard

For RMS, after squaring, averaging technique may be RC LP filter.

for VU meter standard, RC = 0.0651442s equiv 2.44312 Hz

for loudness RMS (LKFS standard) RC = 0.392323 equiv 0.405674 Hz

https://en.wikipedia.org/wiki/A-weighting

https://www.itu.int/dms_pubrec/itu-r/rec/bs/R-REC-BS.1770-3-201208-S!!PDF-E.pdf

https://www.dsprelated.com/freebooks/filters/Low_High_Shelving_Filters.html

https://en.wikipedia.org/wiki/LKFS