pysparse.representation package¶
Submodules¶
pysparse.representation.dcts module¶
-
pysparse.representation.dcts.
dct1
(x, axis=0)[source]¶ 1-Dimension Discrete cosine transform
The DCT of signal \(x[n], n=0, 1,\cdots, N-1\) is expressed as
(1)¶\[y[k] = {\rm DCT}(x[n]) = \left\{ {\begin{array}{lll} {\sqrt{\frac{2}{N}}\sum_{n=0}^{N-1}x[n]\frac{1}{\sqrt 2}, \quad k=0}\\ {\sqrt{\frac{2}{N}}\sum_{n=0}^{N-1}x[n]{\rm cos}\frac{(2n + 1)k\pi}{2N}, \quad k=1, \cdots, N-1} \end{array}} \right. \]where, \(k=0, 1, \cdots, N-1\)
N. Ahmed, T. Natarajan, and K. R. Rao. Discrete cosine transform. IEEE Transactions on Computers, C-23(1):90–93, 1974. doi:10.1109/T-C.1974.223784
Parameters: x (numpy array) – signal vector or matrix Keyword Arguments: axis (number) – transformation axis when x is a matrix (default: {0}, col) Returns: y – the coefficients. Return type: numpy array
-
pysparse.representation.dcts.
dct2
(X)[source]¶ 2-Dimension Discrete cosine transform
dct1(dct1(X, axis=0), axis=1)
Parameters: X (numpy array) – signal matrix Returns: Y – coefficients matrix Return type: numpy array
-
pysparse.representation.dcts.
dctdict
(N, isnorm=False, verbose=False)[source]¶ Complete DCT dictionary
(2)¶\[{\bm D} = \sqrt{\frac{2}{N}}\left[\begin{array}{cccc}{1/\sqrt{2}} & {1/\sqrt{2}} & {1/\sqrt{2}} & {\cdots} & {1/\sqrt{2}} \\ {\cos \frac{\pi}{2 N}} & {\cos \frac{3 \pi}{2 N}} & {\cos \frac{5 \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1) \pi}{2 N}} \\ {\vdots} & {\vdots} & {\vdots} & {\vdots} \\ {\cos \frac{(N-1) \pi}{2 N}} & {\cos \frac{3(N-1) \pi}{2 N}} & {\cos \frac{5(N-1) \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1)(N-1) \pi}{2 N}}\end{array}\right] \]Because \({\bm z} = {\bm D}{\bm x}\), \({\bm D}{\bm D}^T = {\bm I}\) So, \({\bm x} = {\bm D}^T{\bm z}\)
Parameters: N (integer) – The dictionary is of size \(N\times N\) Returns: D – DCT dictionary ( \({\bm D}^T\) ). Return type: numpy array
-
pysparse.representation.dcts.
dctmat
(N)[source]¶ Discrete cosine transform matrix
(3)¶\[{\bm y} = {\bm D}{\bm x} \]where, \({\bm x} = (x_n)_{N\times 1}, x_n = x[n]\), \({\bm D} = (d_{ij})_{N\times N}\) can be expressed as
\[{\bm D} = \sqrt{\frac{2}{N}}\left[\begin{array}{cccc}{1/\sqrt{2}} & {1/\sqrt{2}} & {1/\sqrt{2}} & {\cdots} & {1/\sqrt{2}} \\ {\cos \frac{\pi}{2 N}} & {\cos \frac{3 \pi}{2 N}} & {\cos \frac{5 \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1) \pi}{2 N}} \\ {\vdots} & {\vdots} & {\vdots} & {\vdots} \\ {\cos \frac{(N-1) \pi}{2 N}} & {\cos \frac{3(N-1) \pi}{2 N}} & {\cos \frac{5(N-1) \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1)(N-1) \pi}{2 N}}\end{array}\right] \]Parameters: N (integer) – signal dimesion. Returns: T – DCT matrix. Return type: ndarray
-
pysparse.representation.dcts.
idct1
(y, axis=0)[source]¶ 1-Dimension Inverse Discrete cosine transform
(4)¶\[{\bm x} = {\bm D}^{-1}{\bm y} = {\bm D}^T{\bm y} \]Parameters: y (numpy array) – coefficients Keyword Arguments: axis (number) – IDCT along which axis (default: {0}) Returns: x – recovered signal. Return type: numpy array
-
pysparse.representation.dcts.
idct2
(X)[source]¶ 2-Dimension Inverse Discrete cosine transform
idct1(idct1(X, axis=0), axis=1)
Parameters: X (numpy array) – signal matrix Returns: Y – coefficients matrix Return type: numpy array
-
pysparse.representation.dcts.
odctdict
(dictshape, isnorm=False, verbose=False)[source]¶ Overcomplete 1D-DCT dictionary
(5)¶\[{\bm D} = \sqrt{\frac{2}{N}}\left[\begin{array}{cccc}{1/\sqrt{2}} & {1/\sqrt{2}} & {1/\sqrt{2}} & {\cdots} & {1/\sqrt{2}} \\ {\cos \frac{\pi}{2 N}} & {\cos \frac{3 \pi}{2 N}} & {\cos \frac{5 \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1) \pi}{2 N}} \\ {\vdots} & {\vdots} & {\vdots} & {\vdots} \\ {\cos \frac{(M-1) \pi}{2 N}} & {\cos \frac{3(M-1) \pi}{2 N}} & {\cos \frac{5(M-1) \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1)(M-1) \pi}{2 N}}\end{array}\right] \](6)¶\[{\bm D} = \left[\frac{{\bm d}_0}{\|{\bm d}_0\|_2}, \frac{{\bm d}_1}{\|{\bm d}_1\|_2},\cdots, \frac{{\bm d}_{N-1}}{\|{\bm d}_{N-1}\|_2}\right] \]Parameters: dictshape (tuple) – dictionary shape
Keyword Arguments:
-
pysparse.representation.dcts.
odctndict
(dictshape, axis=-1, isnorm=False, verbose=False)[source]¶ generates Overcomplete nD-DCT dictionary
(7)¶\[{\bm D}_{nd} = {\bm D}_{(n-1)d} \otimes {\bm D}_{(n-1)d}. \]Parameters: dictshape (list or tuple) – shape of DCT dict [M, N]
Keyword Arguments: - axis (number) – Axis along which the dct is computed. If -1 then the transform is multidimensional(default=-1) (default: {-1})
- isnorm (bool) – normlize atoms (default: {False})
- verbose (bool) – display log info (default: {True})
Returns: OD – Overcomplete nD-DCT dictionary
Return type: ndarray
pysparse.representation.dfts module¶
-
pysparse.representation.dfts.
dft1
(x, axis=0)[source]¶ 1-Dimension Discrete cosine transform
The DFT of signal \(x[n], n=0, 1,\cdots, N-1\) is expressed as
(8)¶\[y[k] = {\rm DFT}(x[n]) = \left\{ {\begin{array}{lll} {\sqrt{\frac{2}{N}}\sum_{n=0}^{N-1}x[n]\frac{1}{\sqrt 2}, \quad k=0}\\ {\sqrt{\frac{2}{N}}\sum_{n=0}^{N-1}x[n]{\rm cos}\frac{(2n + 1)k\pi}{2N}, \quad k=1, \cdots, N-1} \end{array}} \right. \]where, \(k=0, 1, \cdots, N-1\)
N. Ahmed, T. Natarajan, and K. R. Rao. Discrete cosine transform. IEEE Transactions on Computers, C-23(1):90–93, 1974. doi:10.1109/T-C.1974.223784
Parameters: x (numpy array) – signal vector or matrix Keyword Arguments: axis (number) – transformation axis when x is a matrix (default: {0}, col) Returns: y – the coefficients. Return type: numpy array
-
pysparse.representation.dfts.
dft2
(X)[source]¶ 2-Dimension Discrete cosine transform
dft1(dft1(X, axis=0), axis=1)
Parameters: X (numpy array) – signal matrix Returns: Y – coefficients matrix Return type: numpy array
-
pysparse.representation.dfts.
dftdict
(N, isnorm=False, verbose=False)[source]¶ Complete DFT dictionary
(9)¶\[{\bm D} = \sqrt{\frac{2}{N}}\left[\begin{array}{cccc}{1/\sqrt{2}} & {1/\sqrt{2}} & {1/\sqrt{2}} & {\cdots} & {1/\sqrt{2}} \\ {\cos \frac{\pi}{2 N}} & {\cos \frac{3 \pi}{2 N}} & {\cos \frac{5 \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1) \pi}{2 N}} \\ {\vdots} & {\vdots} & {\vdots} & {\vdots} \\ {\cos \frac{(N-1) \pi}{2 N}} & {\cos \frac{3(N-1) \pi}{2 N}} & {\cos \frac{5(N-1) \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1)(N-1) \pi}{2 N}}\end{array}\right] \]Because \({\bm z} = {\bm D}{\bm x}\), \({\bm D}{\bm D}^T = {\bm I}\) So, \({\bm x} = {\bm D}^T{\bm z}\)
Parameters: N (integer) – The dictionary is of size \(N\times N\) Returns: D – DFT dictionary ( \({\bm D}^T\) ). Return type: numpy array
-
pysparse.representation.dfts.
dftmat
(N)[source]¶ Discrete Fourier transform matrix
(10)¶\[{\bm y} = {\bm D}{\bm x} \]where, \({\bm x} = (x_n)_{N\times 1}, x_n = x[n]\), \({\bm D} = (d_{ij})_{N\times N}\) can be expressed as
\[{\bm D} = \sqrt{\frac{2}{N}}\left[\begin{array}{cccc}{1/\sqrt{2}} & {1/\sqrt{2}} & {1/\sqrt{2}} & {\cdots} & {1/\sqrt{2}} \\ {\cos \frac{\pi}{2 N}} & {\cos \frac{3 \pi}{2 N}} & {\cos \frac{5 \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1) \pi}{2 N}} \\ {\vdots} & {\vdots} & {\vdots} & {\vdots} \\ {\cos \frac{(N-1) \pi}{2 N}} & {\cos \frac{3(N-1) \pi}{2 N}} & {\cos \frac{5(N-1) \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1)(N-1) \pi}{2 N}}\end{array}\right] \]Parameters: N (integer) – signal dimesion. Returns: T – DFT matrix. Return type: ndarray
-
pysparse.representation.dfts.
idft1
(y, axis=0)[source]¶ 1-Dimension Inverse Discrete cosine transform
(11)¶\[{\bm x} = {\bm D}^{-1}{\bm y} = {\bm D}^T{\bm y} \]Parameters: y (numpy array) – coefficients Keyword Arguments: axis (number) – IDFT along which axis (default: {0}) Returns: x – recovered signal. Return type: numpy array
-
pysparse.representation.dfts.
idft2
(X)[source]¶ 2-Dimension Inverse Discrete cosine transform
idft1(idft1(X, axis=0), axis=1)
Parameters: X (numpy array) – signal matrix Returns: Y – coefficients matrix Return type: numpy array
-
pysparse.representation.dfts.
odftdict
(dictshape, isnorm=False, verbose=False)[source]¶ Overcomplete 1D-DFT dictionary
(12)¶\[{\bm D} = \sqrt{\frac{2}{N}}\left[\begin{array}{cccc}{1/\sqrt{2}} & {1/\sqrt{2}} & {1/\sqrt{2}} & {\cdots} & {1/\sqrt{2}} \\ {\cos \frac{\pi}{2 N}} & {\cos \frac{3 \pi}{2 N}} & {\cos \frac{5 \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1) \pi}{2 N}} \\ {\vdots} & {\vdots} & {\vdots} & {\vdots} \\ {\cos \frac{(M-1) \pi}{2 N}} & {\cos \frac{3(M-1) \pi}{2 N}} & {\cos \frac{5(M-1) \pi}{2 N}} & {\cdots} & {\cos \frac{(2 N-1)(M-1) \pi}{2 N}}\end{array}\right] \](13)¶\[{\bm D} = \left[\frac{{\bm d}_0}{\|{\bm d}_0\|_2}, \frac{{\bm d}_1}{\|{\bm d}_1\|_2},\cdots, \frac{{\bm d}_{N-1}}{\|{\bm d}_{N-1}\|_2}\right] \]Parameters: dictshape (tuple) – dictionary shape
Keyword Arguments:
-
pysparse.representation.dfts.
odftndict
(dictshape, axis=-1, isnorm=False, verbose=False)[source]¶ generates Overcomplete nD-DFT dictionary
(14)¶\[{\bm D}_{nd} = {\bm D}_{(n-1)d} \otimes {\bm D}_{(n-1)d}. \]Parameters: dictshape (list or tuple) – shape of DFT dict [M, N]
Keyword Arguments: - axis (number) – Axis along which the dft is computed. If -1 then the transform is multidimensional(default=-1) (default: {-1})
- isnorm (bool) – normlize atoms (default: {False})
- verbose (bool) – display log info (default: {True})
Returns: OD – Overcomplete nD-DFT dictionary
Return type: ndarray