Signal Decomposition Using EVD of Hankel Matrices

Suppose we are given a data signal which consists of several nearly mono-components.

Can we recover the mono-components?

The answer is YES, with an efficient algorithm using EVDs of Hankel matrices.

Mono-component recovery can be successfully applied to audio signals.

Prerequisites

The reader should be familiar to elementary concepts about signals, and with linear algebra concepts, particularly EVD and its properties and algorithms.

Competences

The reader should be able to decompose given signal into mono-components using EVD methods.

References

For more details see P. Jain and R. B. Pachori, An iterative approach for decomposition of multi-component non-stationary signals based on eigenvalue decomposition of the Hankel matrix.

Credits: The first Julia implementation was derived in A. M. Bačak, Master's Thesis.

Extraction of stationary mono-components

Definitions

Let $x\in\mathbb{R}^{m}$, denote a signal with $N$ samples.

Assume $x$ is composed of $L$ stationary mono-components:

$$ x=\sum\limits_{k=1}^L x^{(k)}, $$

where

$$ x^{(k)}_i=A_k \cos(2\pi f_k i +\theta_k)\quad i=1,2,\ldots,m. $$

Here:

$f_k=\displaystyle\frac{F_k}{F}$ is the normalized frequency of $x^{(k)}$,

$F$ is the sampling frequency of $x$ in Hz,

$F_k$ is the sampling frequency of $x^{(k)}$,

$A_k$ is the amplitude of $x^{(k)}$, and

$\theta_k$ is the phase of $x^{(k)}$.

We assume that $F_k< F_{k+1}$ for $k=1,2,\ldots,n-1$, and $F>2F_n$.

A Hankel matrix is a (real) square matrix with constant values along the skew-diagonals. More precisely, let $a\in\mathbb{R}^{2n-1}$. An $n\times n$ matrix $H\equiv H(a)$ for which $H_{ij}=H_{i+1,j-1}=a_{i+j-1}$ is a Hankel matrix.

Facts

Let $x$ be a signal with $2n-1$ samples composed of $L$ stationary mono-components.

Let $H$ be an $n\times n$ Hankel matrix corresponding to $x$ and let $H=U\Lambda U^T$ be its EVD (Hankel matrix is symmetric) with $\lambda_1\leq \lambda_2 \leq \cdots \leq \lambda_n$.

Smilarly, let $H_k$ be the $n\times n$ Hankel matrix corresponding to the $k$-th component $x^{(k)}$ and let $H_k=U_k\Lambda_k U_k^T$ be its EVD.

  1. $H=\sum\limits_{k=1}^{L} H_k$.

  2. $H_k=\lambda_k U_{:,k}U_{:,k}^T + \lambda_{n-k+1} U_{:,n-k+1}U_{:,n-k+1}^T$.

Example - Signal with three mono-components


In [1]:
using Plots
using SpecialMatrices

In [2]:
# Small Hankel matrix
a=collect(1:11)
Hankel(a)


Out[2]:
6×6 Hankel{Int64}:
 1  2  3  4   5   6
 2  3  4  5   6   7
 3  4  5  6   7   8
 4  5  6  7   8   9
 5  6  7  8   9  10
 6  7  8  9  10  11

In [2]:
# Create the signal
n=160
N=2*n-1
F = 6400
L = 3
A = [3, 2, 1]
Fk= [200, 320, 160]
θ = [pi/2, pi/4, 0]
x = zeros(N)
for k=1:3
    for i=1:N
        x[i]+=A[k]*cos(2*pi*Fk[k]*i/F+θ[k])
    end
end
plot(x,xlabel="Number of samples N", ylabel="Amplitude")


Out[2]:

In [3]:
F/Fk[1]


Out[3]:
32.0

In [4]:
# FFT indicates that there are three components
# The plot of abs.(y[1:end]) is symmetric arround midpoint 
using FFTW
y=fft(x)
plot(log10.(abs.(y))[1:100])


Out[4]:

In [5]:
x


Out[5]:
319-element Array{Float64,1}:
  1.310398374025847
  0.11587514928034615
 -1.0885731049508998
 -2.2202843486637884
 -3.2015156180941835
 -3.965866393618122
 -4.46374202266042
 -4.666359686815328
 -4.567934424546196
 -4.185852159906956
 -3.5588243014269603
 -2.7432062680150535
 -1.8078322687178918
  ⋮
 -0.16307333200557833
  0.5559613234906733
  1.3574250351607462
  2.1908093067708143
  2.996148064294471
  3.709215271029653
  4.267404849305426
  4.615729180467272
  4.712350386311305
  4.5330939044374405
  4.0744834945807185
  3.354972355020254

In [6]:
# Let us decompose the signal 
H=Hankel(x)


Out[6]:
160×160 Hankel{Float64}:
  1.3104     0.115875  -1.08857   …   4.07448    3.35497    2.41421
  0.115875  -1.08857   -2.22028       3.35497    2.41421    1.3104
 -1.08857   -2.22028   -3.20152       2.41421    1.3104     0.115875
 -2.22028   -3.20152   -3.96587       1.3104     0.115875  -1.08857
 -3.20152   -3.96587   -4.46374       0.115875  -1.08857   -2.22028
 -3.96587   -4.46374   -4.66636   …  -1.08857   -2.22028   -3.20152
 -4.46374   -4.66636   -4.56793      -2.22028   -3.20152   -3.96587
 -4.66636   -4.56793   -4.18585      -3.20152   -3.96587   -4.46374
 -4.56793   -4.18585   -3.55882      -3.96587   -4.46374   -4.66636
 -4.18585   -3.55882   -2.74321      -4.46374   -4.66636   -4.56793
 -3.55882   -2.74321   -1.80783   …  -4.66636   -4.56793   -4.18585
 -2.74321   -1.80783   -0.827855     -4.56793   -4.18585   -3.55882
 -1.80783   -0.827855   0.121836     -4.18585   -3.55882   -2.74321
  ⋮                               ⋱                        
  0.555961   1.35743    2.19081      -1.22175   -0.762656  -0.163073
  1.35743    2.19081    2.99615      -0.762656  -0.163073   0.555961
  2.19081    2.99615    3.70922   …  -0.163073   0.555961   1.35743
  2.99615    3.70922    4.2674        0.555961   1.35743    2.19081
  3.70922    4.2674     4.61573       1.35743    2.19081    2.99615
  4.2674     4.61573    4.71235       2.19081    2.99615    3.70922
  4.61573    4.71235    4.53309       2.99615    3.70922    4.2674
  4.71235    4.53309    4.07448   …   3.70922    4.2674     4.61573
  4.53309    4.07448    3.35497       4.2674     4.61573    4.71235
  4.07448    3.35497    2.41421       4.61573    4.71235    4.53309
  3.35497    2.41421    1.3104        4.71235    4.53309    4.07448
  2.41421    1.3104     0.115875      4.53309    4.07448    3.35497

In [7]:
using LinearAlgebra
λ,U=eigen(Matrix(H))
λ


Out[7]:
160-element Array{Float64,1}:
 -239.9999999999991
 -160.0000000000001
  -79.99999999999949
   -6.954988337803782e-13
   -5.3290705182007514e-14
   -3.907186464613127e-14
   -2.9788712322638554e-14
   -2.683129082722271e-14
   -2.5986385299720367e-14
   -2.4939524503263148e-14
   -2.4424906541753444e-14
   -2.1842051486212786e-14
   -2.1760371282653068e-14
    ⋮
    2.205264994553126e-14
    2.2091712020148427e-14
    2.5564532223176637e-14
    2.990786304165372e-14
    3.019806626980426e-14
    3.126737327296269e-14
    3.4724609786192e-14
    5.0182080713057076e-14
    7.875057712690488e-14
   80.00000000000014
  160.00000000000014
  240.0

We see that the three smallest and the three largest eigenvalues come in pairs and define the three mono-components.

The ratios of the moduli of the eigenvalues correspond to the ratios of the amplitudes of the mono-components.


In [10]:
# Form the three matrices
Hcomp=Array{Any}(undef,3)
for k=1:L
    Hcomp[k]=λ[k]*U[:,k]*U[:,k]' + λ[end-k+1]*U[:,end-k+1]*U[:,end-k+1]'
end

In [11]:
# Compare the first matrix with the Hankel matrix of the first mono-component
x₁ = zeros(N)
l=1
for i=1:N
    x₁[i]+=A[l]*cos(2*pi*Fk[l]*i/F+θ[l])
end

In [12]:
H₁=Hankel(x₁)
eigvals(Matrix(H₁)), norm(Hcomp[1]-H₁)


Out[12]:
([-240.00000000000003, -1.8548143693448077e-13, -1.7935792379859621e-13, -1.6688127578266626e-13, -1.641487230267321e-13, -1.59940795832411e-13, -1.3772765361135672e-13, -1.3283079246869393e-13, -1.166491001750867e-13, -1.1579933954148485e-13  …  1.1314344928452745e-13, 1.1753910153785377e-13, 1.2207964424188983e-13, 1.373320670432502e-13, 1.5801973069883012e-13, 1.6026707175422477e-13, 1.695262875339962e-13, 1.7972425885967524e-13, 1.8300277485018596e-13, 240.00000000000009], 1.6680439311773043e-12)

In [13]:
# Now we reconstruct the mono-components from the skew-diagonal elements of Hcomp
xcomp=Array{Array{Float64}}(undef,L)
z=Array{Float64}(undef,N)
for k=1:L
    z[1:2:N]=diag(Hcomp[k])
    z[2:2:N]=diag(Hcomp[k],1)
    xcomp[k]=copy(z)
end

In [14]:
xaxis=collect(1:N)
plot([xcomp[1],xcomp[2],xcomp[3]])


Out[14]:

Fast EVD of Hankel matrices

Several outer eigenvalues pairs of Hankel matrices can be computed using Lanczos method. If the multiplication $Hx$ is performed using Fast Fourier Transform, this EVD computation is very fast.

Definitions

A Toeplitz matrix is a (real) square matrix with constant values along the diagonals. More precisely, let

$$ a=(a_{-(n-1)},a_{-(n-2)},\ldots,a_{-1},a_0,a_1,\ldots,a_{n-1})\in\mathbb{R}^{2n-1}. $$

An $n\times n$ matrix $T\equiv T(a)$ for which $T_{ij}=T_{i+1,j+1}=a_{i-j}$ is a Toeplitz matrix.

A circulant matrix is a Toeplitz matrix where each column is rotated one element downwards relative to preceeding column.

More precisely, let $a\in\mathbb{R}^{n}$. An $n\times n$ matrix $C\equiv C(a)=T(a,a_{1:n-1})$ is a Circulant matrix.

A rotation matrix is an identity matrix rotated 90 degrees to the right (or left).

A Fourier matrix is Vandermonde matrix

$$ F_n=V(1,\omega_n,\omega_n^2,\ldots, \omega_n^{n-1}), $$

where $\omega_n=exp(2\pi i/n)$ is the $n$-th root of unity (see the notebook Eigenvalue Decomposition - Definitions and Facts).

Example

Notice different meanings of vector $a$: in C=Circulant(a), $a$ is the first column, in T=Toeplitz(a), $a_i$ is the diagonal element of the $i$-th diagonal starting from $T_{1n}$, and in H=Hankel(a), $a_i$ is the element of the $i$-th skew-diagonal starting from $H_{11}$.


In [15]:
C=Circulant([1,2,3,4,5])


Out[15]:
5×5 Circulant{Int64}:
 1  5  4  3  2
 2  1  5  4  3
 3  2  1  5  4
 4  3  2  1  5
 5  4  3  2  1

In [16]:
TC=Toeplitz([2,3,4,5,1,2,3,4,5])


Out[16]:
5×5 Toeplitz{Int64}:
 1  5  4  3  2
 2  1  5  4  3
 3  2  1  5  4
 4  3  2  1  5
 5  4  3  2  1

In [17]:
T=Toeplitz([1,2,3,4,5,6,7,8,9])


Out[17]:
5×5 Toeplitz{Int64}:
 5  4  3  2  1
 6  5  4  3  2
 7  6  5  4  3
 8  7  6  5  4
 9  8  7  6  5

In [18]:
H₁=Hankel([1,2,3,4,5,6,7,8,9])


Out[18]:
5×5 Hankel{Int64}:
 1  2  3  4  5
 2  3  4  5  6
 3  4  5  6  7
 4  5  6  7  8
 5  6  7  8  9

In [19]:
Vandermonde([6,2,3,4,5])


Out[19]:
5×5 Vandermonde{Int64}:
 1  6  36  216  1296
 1  2   4    8    16
 1  3   9   27    81
 1  4  16   64   256
 1  5  25  125   625

Facts

For more details see G. H. Golub and C. F. Van Loan, Matrix Computations, p. 202, and the references therein

  1. Hankel matrix is the product of a Toeplitz matrix and the rotation matrix.

  2. Circulant matrix is normal and, thus, unitarily diagonalizable, with the eigenvalue decomposition $$ C(a)=U\mathop{\mathrm{diag}}(F_n^* a)U^*, $$ where $U=\displaystyle\frac{1}{\sqrt{n}} F_n$. The product $F_n^* a$ can be computed by the Fast Fourier Transform(FFT).

  3. Given $a,x\in\mathbb{R}^n$, the product $y=C(a)x$ can be computed using FFT as follows: \begin{align*} \tilde x&=F_n^*x\\ \tilde a&=F_n^*a\\ \tilde y&=\tilde x.* \tilde a\\ y&= F_n^{-*} \tilde y. \end{align*}

  4. Toeplitz matrix of order $n$ can be embedded in a circulant matrix of order $2n-1$: if $a\in\mathbb{R}^{2n-1}$, then $$ T(a)=[C([a_{n+1:2n-1};a_{1:n}])]_{1:n,1:n}. $$

  5. Further, let $x\in\mathbb{R}^{n}$ and let $\bar x\in\mathbb{R}^{2n-1}$ be equal to $x$ padded with $n-1$ zeros.Then $$ T(a)x=[C([a_{n+1:2n-1};a_{1:n}])\bar x]_{1:n}. $$

  6. Fact 1 implies $H(a)x=(T(a)J)x=T(a)(Jx)$.

Examples


In [20]:
# Fact 1
eye(n)=Matrix{Int64}(I,n,n)
J=rotl90(eye(5))
T*J


Out[20]:
5×5 Array{Int64,2}:
 1  2  3  4  5
 2  3  4  5  6
 3  4  5  6  7
 4  5  6  7  8
 5  6  7  8  9

In [21]:
J


Out[21]:
5×5 Array{Int64,2}:
 0  0  0  0  1
 0  0  0  1  0
 0  0  1  0  0
 0  1  0  0  0
 1  0  0  0  0

In [22]:
rotl90(T)


Out[22]:
5×5 Array{Int64,2}:
 1  2  3  4  5
 2  3  4  5  6
 3  4  5  6  7
 4  5  6  7  8
 5  6  7  8  9

In [23]:
# Fact 1
Matrix(T)*J


Out[23]:
5×5 Array{Int64,2}:
 1  2  3  4  5
 2  3  4  5  6
 3  4  5  6  7
 4  5  6  7  8
 5  6  7  8  9

In [24]:
# Fact 2
import Random
Random.seed!(467)
a=rand(-8:8,6)
n=length(a)
C=Circulant(a)
ω=exp(2*pi*im/n)
v=[ω^k for k=0:n-1]
F=Vandermonde(v)
U=F/sqrt(n)
λ=Matrix(F)'*a


Out[24]:
6-element Array{Complex{Float64},1}:
                 1.0 + 0.0im
 -15.000000000000002 + 3.4641016151377513im
  -7.999999999999998 + 1.7320508075688732im
   9.000000000000004 + 1.7486012637846216e-14im
   -8.00000000000001 - 1.7320508075688812im
 -14.999999999999993 - 3.464101615137774im

In [25]:
a


Out[25]:
6-element Array{Int64,1}:
 -6
 -4
  5
  1
  6
 -1

In [26]:
fft(a)


Out[26]:
6-element Array{Complex{Float64},1}:
   1.0 + 0.0im
 -15.0 + 3.4641016151377544im
  -8.0 + 1.7320508075688772im
   9.0 + 0.0im
  -8.0 - 1.7320508075688772im
 -15.0 - 3.4641016151377544im

In [27]:
eigvals(Matrix(C))


Out[27]:
6-element Array{Complex{Float64},1}:
 -14.99999999999999 - 3.464101615137751im
 -14.99999999999999 + 3.464101615137751im
               -8.0 - 1.7320508075688776im
               -8.0 + 1.7320508075688776im
 0.9999999999999999 + 0.0im
                9.0 + 0.0im

In [28]:
v


Out[28]:
6-element Array{Complex{Float64},1}:
                 1.0 + 0.0im
  0.5000000000000001 + 0.8660254037844386im
 -0.4999999999999998 + 0.8660254037844388im
                -1.0 + 3.885780586188048e-16im
 -0.5000000000000006 - 0.8660254037844385im
 0.49999999999999944 - 0.8660254037844392im

In [29]:
F


Out[29]:
6×6 Vandermonde{Complex{Float64}}:
 1.0+0.0im   1.0+0.0im           1.0+0.0im          …   1.0+0.0im
 1.0+0.0im   0.5+0.866025im     -0.5+0.866025im         0.5-0.866025im
 1.0+0.0im  -0.5+0.866025im     -0.5-0.866025im        -0.5-0.866025im
 1.0+0.0im  -1.0+3.88578e-16im   1.0-7.77156e-16im     -1.0+1.94289e-15im
 1.0+0.0im  -0.5-0.866025im     -0.5+0.866025im        -0.5+0.866025im
 1.0+0.0im   0.5-0.866025im     -0.5-0.866025im     …   0.5+0.866025im

In [30]:
C


Out[30]:
6×6 Circulant{Int64}:
 -6  -1   6   1   5  -4
 -4  -6  -1   6   1   5
  5  -4  -6  -1   6   1
  1   5  -4  -6  -1   6
  6   1   5  -4  -6  -1
 -1   6   1   5  -4  -6

In [31]:
# Residual
norm(Matrix(C)*U-U*Diagonal(λ))


Out[31]:
4.129481218738273e-14

In [32]:
?fft;

In [33]:
# Check fft
norm(λ-fft(a))


Out[33]:
3.020260682225245e-14

Fact 3 - Circulant() x vector, as implemented in the package SpecialMatrices.jl

function *(C::Circulant{T},x::Vector{T}) where T
    xt=fft(x)
    vt=fft(C.c)
    yt=vt.*xt
    real(ifft(yt))
end

Similarly, mul!()

function mul!(y::StridedVector{T},C::Circulant{T},x::StridedVector{T}) where T
    xt=fft(x)
    vt=fft(C.c)
    yt=ifft(vt.*xt)
    if T<: Int
        map!(round,y,yt) 
    elseif T<: Real
        map!(real,y,yt)
    else
        copy!(y,yt)
    end
    return y
end

In [34]:
x=rand(-9:9,n)


Out[34]:
6-element Array{Int64,1}:
 -6
  1
 -9
 -4
 -7
  9

In [35]:
@which C*x


Out[35]:
*(C::Circulant{T}, x::Array{T,1}) where T in SpecialMatrices at C:\Users\Ivan_Slapnicar\.julia\packages\SpecialMatrices\Gtzb0\src\toeplitz.jl:55

In [36]:
[Matrix(C)*x C*x mul!(similar(x),C,x)]


Out[36]:
6×3 Array{Int64,2}:
 -94  -94  -94
  41   41   41
  -9   -9   -9
 120  120  120
 -31  -31  -31
 -43  -43  -43

In [37]:
# Fact 4 - Embedding Toeplitz() into Circulant()
n=5
a=rand(-6:6,2*n-1)
T=Toeplitz(a)


Out[37]:
5×5 Toeplitz{Int64}:
 -1  -6   5   1   3
 -6  -1  -6   5   1
  2  -6  -1  -6   5
 -5   2  -6  -1  -6
  1  -5   2  -6  -1

In [38]:
C=Circulant([a[n:2*n-1];a[1:n-1]])


Out[38]:
9×9 Circulant{Int64}:
 -1  -6   5   1   3   1  -5   2  -6
 -6  -1  -6   5   1   3   1  -5   2
  2  -6  -1  -6   5   1   3   1  -5
 -5   2  -6  -1  -6   5   1   3   1
  1  -5   2  -6  -1  -6   5   1   3
  3   1  -5   2  -6  -1  -6   5   1
  1   3   1  -5   2  -6  -1  -6   5
  5   1   3   1  -5   2  -6  -1  -6
 -6   5   1   3   1  -5   2  -6  -1

In [39]:
# Fact 5 - Toeplitz() x vector
x=rand(-6:6,n)


Out[39]:
5-element Array{Int64,1}:
 -2
  6
 -4
 -5
  4
$$ \begin{bmatrix} T & A \\ B & C\end{bmatrix} \begin{bmatrix} x \\ 0 \end{bmatrix} $$

In [40]:
[Matrix(T)*x T*x mul!(similar(x),T,x)]


Out[40]:
5×3 Array{Int64,2}:
 -47  -47  -47
   9    9    9
  14   14   14
  27   27   27
 -14  -14  -14

In [43]:
# Fact 6 - Hankel() x vector
H₁=Hankel(a)


Out[43]:
5×5 Hankel{Int64}:
  3   1   5  -6  -1
  1   5  -6  -1  -6
  5  -6  -1  -6   2
 -6  -1  -6   2  -5
 -1  -6   2  -5   1

In [44]:
[Matrix(H₁)*x H₁*x mul!(similar(x),H₁,x)]


Out[44]:
5×3 Array{Int64,2}:
   6    6    6
  33   33   33
  -4   -4   -4
   0    0    0
 -13  -13  -13

Example - Fast EVD of a Hankel matrix

Given a Hankel matrix $H$, the Lanczos method can be applied by defining a function (linear map) which returns the product $Hx$ for any vector $x$. This approach uses the package LinearMaps.jl and is described in the notebook Symmetric Eigenvalue Decomposition - Lanczos Method notebook.

The computation is very fast and allocates little extra space.


In [45]:
# import Pkg; Pkg.add("LinearMaps")

In [46]:
using LinearMaps

In [47]:
n=size(H,1)
f(x)=mul!(similar(x),H,x)


Out[47]:
f (generic function with 1 method)

In [48]:
H


Out[48]:
160×160 Hankel{Float64}:
  1.3104     0.115875  -1.08857   …   4.07448    3.35497    2.41421
  0.115875  -1.08857   -2.22028       3.35497    2.41421    1.3104
 -1.08857   -2.22028   -3.20152       2.41421    1.3104     0.115875
 -2.22028   -3.20152   -3.96587       1.3104     0.115875  -1.08857
 -3.20152   -3.96587   -4.46374       0.115875  -1.08857   -2.22028
 -3.96587   -4.46374   -4.66636   …  -1.08857   -2.22028   -3.20152
 -4.46374   -4.66636   -4.56793      -2.22028   -3.20152   -3.96587
 -4.66636   -4.56793   -4.18585      -3.20152   -3.96587   -4.46374
 -4.56793   -4.18585   -3.55882      -3.96587   -4.46374   -4.66636
 -4.18585   -3.55882   -2.74321      -4.46374   -4.66636   -4.56793
 -3.55882   -2.74321   -1.80783   …  -4.66636   -4.56793   -4.18585
 -2.74321   -1.80783   -0.827855     -4.56793   -4.18585   -3.55882
 -1.80783   -0.827855   0.121836     -4.18585   -3.55882   -2.74321
  ⋮                               ⋱                        
  0.555961   1.35743    2.19081      -1.22175   -0.762656  -0.163073
  1.35743    2.19081    2.99615      -0.762656  -0.163073   0.555961
  2.19081    2.99615    3.70922   …  -0.163073   0.555961   1.35743
  2.99615    3.70922    4.2674        0.555961   1.35743    2.19081
  3.70922    4.2674     4.61573       1.35743    2.19081    2.99615
  4.2674     4.61573    4.71235       2.19081    2.99615    3.70922
  4.61573    4.71235    4.53309       2.99615    3.70922    4.2674
  4.71235    4.53309    4.07448   …   3.70922    4.2674     4.61573
  4.53309    4.07448    3.35497       4.2674     4.61573    4.71235
  4.07448    3.35497    2.41421       4.61573    4.71235    4.53309
  3.35497    2.41421    1.3104        4.71235    4.53309    4.07448
  2.41421    1.3104     0.115875      4.53309    4.07448    3.35497

In [49]:
A=LinearMap(f,n,issymmetric=true)


Out[49]:
LinearMaps.FunctionMap{Float64}(f, 160, 160; ismutating=false, issymmetric=true, ishermitian=true, isposdef=false)

In [50]:
size(A)


Out[50]:
(160, 160)

In [51]:
@time eigvals(Matrix(H));


  0.007983 seconds (174 allocations: 672.188 KiB)

In [52]:
# import Pkg; Pkg.add("Arpack")

In [53]:
using Arpack

In [55]:
# Run twice
@time λA,UA=eigs(A, nev=6, which=:LM)


  0.028216 seconds (29.91 k allocations: 4.042 MiB)
Out[55]:
([-239.9999999999999, 239.99999999999983, -160.00000000000014, 160.00000000000009, 80.0, -79.99999999999997], [-0.08642519605185052 -0.07092732539296263 … -0.11145874630875588 0.008771993575031174; -0.09860179489427416 -0.05270375739572546 … -0.10871426206426321 0.026099985130698854; … ; -0.05270375739572564 -0.09860179489427408 … -0.10871426206426317 -0.026099985130698992; -0.07092732539296275 -0.08642519605185037 … -0.11145874630875587 -0.008771993575031454], 6, 1, 20, [5.9283317585092165e-15, 8.1810831469895e-16, -2.9113746834453143e-15, 6.739203965471813e-15, 7.613863284140081e-15, 2.412802319590651e-15, -3.392618710661375e-15, -8.03346016662936e-15, -3.0989477118481287e-15, 9.94064796291618e-15  …  6.842027859665681e-15, 1.0877984164970722e-14, -1.7340120244932566e-14, 4.816537701324622e-15, 1.9637282643617705e-16, 1.0549017304809336e-14, 1.041382657328314e-15, 1.43176962062991e-14, 1.860437910466894e-14, 1.1841106898887215e-14])

Extraction of non-stationary mono-components

Definitions

Let $x\in\mathbb{R}^{m}$, denote a signal with $N$ samples.

Assume $x$ is composed of $L$ non-stationary mono-components:

$$ x=\sum\limits_{k=1}^L x^{(k)}, $$

where

$$ x^{(k)}_i=A_k \cos(2\pi f_k i +\theta_k),\quad i=1,2,\ldots,m. $$

Assume that the normalized frequencies $f_k=\displaystyle\frac{F_k}{F}$, the sampling frequencies $F_k$, the amplitudes $A_k$, and the phases $\theta_k$, all sightly change in time.

Let $H\equiv H(x)$ be the Hankel matrix of $x$. The eigenpair of $(\lambda,u)$ of $H$ is significant if $|\lambda|> \tau \cdot \sigma(H)$. Here $\sigma(H)$ is the spectral radius of $H$, and $\tau$ is the significant threshold percentage chosen by the user depending on the type of the problem.

Fact

The following algorithm decomposes the signal $x$:

  1. Choose $\tau$ and form the Hankel matrix $H$
  2. Compute the EVD of $H$
  3. Choose the significant eigenpairs of $H$
  4. For each significant eigenpair $(\lambda,u)$
    1. Form the rank one matrix $M=\lambda uu^T$
    2. Define a new signal $y$ consisting of averages of the skew-diagonals of $M$
    3. Form the Hankel matrix $H(y)$
    4. Compute the EVD of $H(y)$
    5. Choose the significant eigenpairs of $H(y)$
    6. If $H(y)$ has only two significant eigenpairs, declare $y$ a mono-component, else go to step 4.

Example - Note A

Each tone has its fundamental frequency (mono-component). However, musical instruments produce different overtones (harmonics) which are near integer multiples of the fundamental frequency. Due to construction of resonant boxes, these frequencies slightly vary in time, and their amplitudes are contained in a time varying envelope.

Tones produces by musical instruments are nice examples of non-stationary signals. We shall decompose the note A4 played on piano.

For manipulation of recordings, we are using package WAV.jl. Another package with similar functionality is the package AudioIO.jl.


In [56]:
# import Pkg; Pkg.add("WAV")

In [57]:
using WAV

In [58]:
varinfo(WAV)


Out[58]:
name size summary
WAV 299.399 KiB Module
WAVArray 80 bytes UnionAll
WAVChunk 200 bytes DataType
WAVE_FORMAT_ALAW 2 bytes UInt16
WAVE_FORMAT_IEEE_FLOAT 2 bytes UInt16
WAVE_FORMAT_MULAW 2 bytes UInt16
WAVE_FORMAT_PCM 2 bytes UInt16
WAVFormat 240 bytes DataType
WAVFormatExtension 208 bytes DataType
WAVMarker 208 bytes DataType
bits_per_sample 0 bytes typeof(bits_per_sample)
isextensible 0 bytes typeof(isextensible)
isformat 0 bytes typeof(isformat)
wav_cue_read 0 bytes typeof(wav_cue_read)
wav_cue_write 0 bytes typeof(wav_cue_write)
wav_info_read 0 bytes typeof(wav_info_read)
wav_info_write 0 bytes typeof(wav_info_write)
wavappend 0 bytes typeof(wavappend)
wavplay 0 bytes typeof(wavplay)
wavread 0 bytes typeof(wavread)
wavwrite 0 bytes typeof(wavwrite)

In [59]:
# Load a signal
Signal = wavread("files/piano_A41.wav")


Out[59]:
([-0.010132145146031068; -0.010254219183935057; … ; 0.0; 0.0], 44100.0f0, 0x0010, WAVChunk[WAVChunk(Symbol("fmt "), UInt8[0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x44, 0xac, 0x00, 0x00, 0x88, 0x58, 0x01, 0x00, 0x02, 0x00, 0x10, 0x00])])

In [60]:
typeof(Signal)


Out[60]:
Tuple{Array{Float64,2},Float32,UInt16,Array{WAVChunk,1}}

In [61]:
s=Signal[1]
Fs=Signal[2]


Out[61]:
44100.0f0

In [62]:
# Play the signal
wavplay(s,Fs)


MethodError: no method matching wavplay(::Array{Float64,2}, ::Float32)
Closest candidates are:
  wavplay(::Any) at C:\Users\Ivan_Slapnicar\.julia\packages\WAV\T2P9V\src\WAV.jl:38

Stacktrace:
 [1] top-level scope at In[62]:1

In [63]:
# Plot the signal
plot(s)


Out[63]:

In [64]:
# Plot in time scale
t=range(0,stop=length(s)/Fs,length=length(s))
plot(t,s,xlabel="Time")


Out[64]:

In [65]:
# Total time and number of samples
t[end], length(s)


Out[65]:
(4.9821315f0, 219712)

In [66]:
# Detail
plot(s[1:800])


Out[66]:

Let us visualize the signal in detail.


In [67]:
k=500
plot(collect(k:k+1000),s[k:k+1000])


Out[67]:

In [68]:
# Last part of the signal is just noise, so we read a 
# shorter signal. N must be odd.
Signal = wavread("files/piano_A41.wav",100001)


Out[68]:
([-0.010132145146031068; -0.010254219183935057; … ; -0.002868739890743736; -0.0030518509475997192], 44100.0f0, 0x0010, WAVChunk[WAVChunk(Symbol("fmt "), UInt8[0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x44, 0xac, 0x00, 0x00, 0x88, 0x58, 0x01, 0x00, 0x02, 0x00, 0x10, 0x00])])

In [69]:
s=Signal[1];

In [70]:
# wavplay(s,Fs)

In [71]:
# File to play on Windows
wavwrite(Signal[1],"files/piano_A41_short.wav",Fs=Signal[2])

In [72]:
# Plot the short signal in time
t=range(0,stop=length(s)/Fs,length=length(s))
plot(t,s)


Out[72]:

In [73]:
# Check the signal with FFT
# Notice 3 stronger harmonics and six more weaker ones
fs=abs.(fft(s))
plot(t,fs)


Out[73]:

In [74]:
# Details
l=10000
plot(t[1:l],fs[1:l])


Out[74]:

In [75]:
# Form the Hankel matrix
# IMPORTANT - Do not try to display H - it is a 50001 x 50001 matrix.
H=Hankel(vec(s));

In [76]:
size(H), H[100,200]


Out[76]:
((50001, 50001), 0.727927488021485)

In [77]:
@time fft(s);


  0.046223 seconds (2.00 k allocations: 3.263 MiB)

In [78]:
# We are looking for 20 eigenvalue pairs
n=size(H,1)
f(x)=mul!(similar(x),H,x)
A=LinearMap(f,n,issymmetric=true)
size(A)


Out[78]:
(50001, 50001)

In [79]:
@time λ,U=eigs(A, nev=40, which=:LM)


 11.270944 seconds (1.21 M allocations: 1.749 GiB, 2.00% gc time)
Out[79]:
([1802.9106277522476, -1799.503617983549, 1297.4493306762442, -1296.2732475837306, -537.8881426869141, 537.758964773014, 416.9063342458265, -415.31980671652036, 410.1794458739445, -410.1577430788212  …  -94.89686455397607, 94.8306120956509, 92.44201905957075, -92.35723643618854, 81.75409375919456, -81.7476982725376, 65.90953909605004, -65.47455750590362, 65.3807439957992, -64.40695210656799], [-0.014728369072220762 -0.00759016526385392 … 0.0009348653318876528 -0.032847917695761245; -0.015181569648502442 -0.0066531422572196925 … -0.004078859302237377 -0.034485300507061514; … ; 0.0017826745607681146 -0.0011107795346319354 … -0.00032698818396558995 -4.123845962249271e-5; 0.0017103207051644169 -0.0012207173279507417 … -0.00033846336404849296 -6.293403730683032e-5], 40, 3, 123, [0.12765943893857185, 0.3079015683267061, 0.49553597392100385, 0.6966582316402534, 0.8883047145032079, 0.9749703779884948, 0.8730728068633765, 0.5904905394553849, 0.2280330112517264, -0.056965377245558446  …  0.006568947562072009, -0.002524994317246402, -0.010278860711971004, -0.01516736109014569, -0.017411440426649702, -0.01757032479309683, -0.018279553932321273, -0.023434929366082873, -0.03414948672346949, -0.049144900396871194])

In [80]:
# Count the eigenvalue pairs (+-) larger than the 10% of the maximum
τ=0.1
L=round(Int,(sum(abs.(λ).>(τ*maximum(abs,λ)))/2))


Out[80]:
11

At this point, the implementation using full matrices is rather obvious. However, we cannot do that, due to large dimension. Recall, the task is to define Hankel matrices $H_k$ for $k=1,\ldots,L$, from the signal obtained by averaging the skew-diagonals of the matrices

$$ H_k=\lambda_k U_{:,k}U_{:,k}^T + \lambda_{n-k+1} U_{:,n-k+1}U_{:,n-k+1}^T, $$

without actually forming the matrices.

This is a nice programming excercise which can be solved using $\cdot$ products.


In [81]:
function myaverages(λ::T, u::Vector{T}) where T
    n=length(u)
    x=Array{Float64}(undef,2*n-1)
    # Average lower diagonals
    for i=1:n
        x[i]=dot(u[1:i],reverse(u[1:i]))/i
    end
    for i=2:n
        x[n+i-1]=dot(u[i:n],reverse(u[i:n]))/(n-i+1)
    end
    λ*x
end


Out[81]:
myaverages (generic function with 1 method)

In [82]:
# A small test
u=[1,2,3,4,5]
u*u'


Out[82]:
5×5 Array{Int64,2}:
 1   2   3   4   5
 2   4   6   8  10
 3   6   9  12  15
 4   8  12  16  20
 5  10  15  20  25

In [83]:
myaverages(1,u)


Out[83]:
9-element Array{Float64,1}:
  1.0
  2.0
  3.3333333333333335
  5.0
  7.0
 11.0
 15.333333333333334
 20.0
 25.0

We now execute the first step of the algorithm from the above Fact.

Notice that eigs() returns the eigenvalues arranged by the absoulte value, so the consecutive pairs define the $i$-th signal. The computation of averages is long - it requires $O(n^2)$ operations and takes several minutes.


In [84]:
# This step takes 7 minutes, so we skip it

# xcomp=Array(Array{Float64},L)
# for k=1:L
#     xcomp[k]=myaverages(λ[2*k-1],U[:,2*k-1])+myaverages(λ[2*k],U[:,2*k])
# end

Can we do without averaging?

The function myaverages() is very slow - 7 minutes, compared to 5 seconds for the eigenvalue computation.

The simplest option is to disregard the averages, and use the first column and the last row of the underlying matrix, as in definition of Hankel matrices, which we do next. Smarter approach might be to use small random samples to compute the averages.

Let us try the simple approach for the fundamental frequency. (See also the notebook Examples in Signal Decomposition.ipynb.)


In [85]:
xcomp=Array{Array{Float64}}(undef,L)
for k=1:L
    k1=2*k-1
    k2=2*k
    xsimple=[(λ[k1]*U[1,k1])*U[:,k1]; (λ[k1]*U[n,k1])*U[2:n,k1]]
    xsimple+=[(λ[k2]*U[1,k2])*U[:,k2]; (λ[k2]*U[n,k2])*U[2:n,k2]]
    xcomp[k]=xsimple
end

Let us look and listen to what we got:


In [86]:
typeof(xcomp[1])


Out[86]:
Array{Float64,1}

In [87]:
k=7
plot(t,xcomp[k])


Out[87]:

In [88]:
k=4
plot(t[1:1000],xcomp[k][1:1000])


Out[88]:

In [89]:
# Plot short first parts of FFTs of and display frequency
l=10000
k=11
fs=abs.(fft(xcomp[k]))
m,ind=findmax(fs[1:l])
println("Frequency = ", ind*Fs/length(fs)  ," Hz, Amplitude = ", m)
plot(t[1:l],fs[1:l])


Frequency = 2645.0916 Hz, Amplitude = 221.7804908011175
Out[89]:

We see that all xcomp[k] are clean mono-components - see Physics of Music - Notes:

1 = 440 Hz (A4)
2 = 880 Hz (2*440,+octave,A5)
3 = 1320 Hz (3*440,+quint,E6)
4 = 440 Hz  
5 = 880 Hz
6 = 2200 Hz (5*440,++major terza, C#7) 
7 = 2640 Hz (6*440,++quint,E7)
8 = 440 Hz
9 = 2200 Hz
10 = 1760 Hz (4*440,++octave,A6)
11 = 2640 Hz

N.B. Some mono-components are repeated, and they should be grouped by adding components with absolute weighted correlation larger than some prescribed threshold.


In [90]:
# wavplay(xcomp[2],Fs)

In [91]:
wavplay(sum([xcomp[i] for i=1:11]),Fs)


MethodError: no method matching wavplay(::Array{Float64,1}, ::Float32)
Closest candidates are:
  wavplay(::Any) at C:\Users\Ivan_Slapnicar\.julia\packages\WAV\T2P9V\src\WAV.jl:38

Stacktrace:
 [1] top-level scope at In[91]:1

In [92]:
# On Windows, store the mono-components
for i=1:11
    wavwrite(xcomp[i],"files/comp$i.wav",Fs=Signal[2])
end

In [93]:
wavwrite(sum([xcomp[i] for i=1:11]),"files/compsum.wav",Fs=Signal[2])