torchcs.dictionary package

Submodules

torchcs.dictionary.dcts module

torchcs.dictionary.dcts.dct1(x, axis=0)

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

tensor (x) – signal vector or matrix

Keyword Arguments

axis (number) – transformation axis when x is a matrix (default: {0}, col)

Returns

the coefficients.

Return type

y tensor

torchcs.dictionary.dcts.dct2(X)

2-Dimension Discrete cosine transform

dct1(dct1(X, axis=0), axis=1)

Parameters

tensor (X) – signal matrix

Returns

coefficients matrix

Return type

Y tensor

torchcs.dictionary.dcts.dctdict(N, isnorm=True, islog=False)

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 (int) – The dictionary is of size \(N\times N\)

Returns

DCT dictionary ( \({\bm D}^T\) ).

Return type

D tensor

torchcs.dictionary.dcts.dctmtx(N)

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

(4)\[{\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 (int) – signal dimesion.

Returns

DCT matrix.

Return type

T tensor

torchcs.dictionary.dcts.idct1(y, axis=0)

1-Dimension Inverse Discrete cosine transform

(5)\[{\bm x} = {\bm D}^{-1}{\bm y} = {\bm D}^T{\bm y} \]
Parameters

tensor (y) – coefficients

Keyword Arguments

axis (number) – IDCT along which axis (default: {0})

Returns

recovered signal.

Return type

x tensor

torchcs.dictionary.dcts.idct2(X)

2-Dimension Inverse Discrete cosine transform

idct1(idct1(X, axis=0), axis=1)

Parameters

tensor (X) – signal matrix

Returns

coefficients matrix

Return type

Y tensor

torchcs.dictionary.dcts.idctmtx(N)

Inverse discrete cosine transform matrix

(6)\[{\bm x} = {\bm D}{\bm z} \]

where, \({\bm x} = (x_n)_{N\times 1}, x_n = x[n]\), \({\bm D}^T = (d_{ij})_{N\times N}\) can be expressed as

(7)\[{\bm D}^T = \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 (int) – signal dimesion.

Returns

IDCT matrix.

Return type

T tensor

torchcs.dictionary.dcts.odctdict(dictshape, isnorm=True, islog=False)

Overcomplete 1D-DCT dictionary

(8)\[{\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] \]
(9)\[{\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
  • isnorm (bool) – normlize atoms (default: True)

  • islog (bool) – display log (default: False)

torchcs.dictionary.dcts.odctndict(dictshape, axis=- 1, isnorm=True, islog=False)

generates Overcomplete nD-DCT dictionary

(10)\[{\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: True)

  • islog (bool) – display log info (default: False)

Returns

OD – Overcomplete nD-DCT dictionary

Return type

torch tensor

torchcs.dictionary.dfts module

torchcs.dictionary.dfts.dft1(x, axis=0)

1-Dimension Discrete Fourier transform

The DFT of signal \(x[n], n=0, 1,\cdots, N-1\) is expressed as

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

torchcs.dictionary.dfts.dft2(X)

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

torchcs.dictionary.dfts.dftdict(N, isnorm=True, islog=False)

Complete DFT dictionary

Parameters

N (integer) – The dictionary is of size \(N\times N\)

Returns

D – DFT dictionary ( \({\bm D}^T\) ).

Return type

numpy array

torchcs.dictionary.dfts.dftmtx(N)

Discrete Fourier transform matrix

(11)\[{\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

Parameters

N (integer) – signal dimesion.

Returns

DFT matrix.

Return type

T tensor

torchcs.dictionary.dfts.idft1(y, axis=0)

1-Dimension Inverse Discrete cosine transform

(12)\[{\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

torchcs.dictionary.dfts.idft2(X)

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

torchcs.dictionary.dfts.idftmtx(N)

Discrete Fourier transform matrix

(13)\[{\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

Parameters

N (integer) – signal dimesion.

Returns

DFT matrix.

Return type

T tensor

torchcs.dictionary.dfts.odftdict(dictshape, isnorm=True, islog=False)

Overcomplete 1D-DFT dictionary

Parameters

dictshape (tuple) – dictionary shape

Keyword Arguments
  • isnorm (bool) – normlize atoms (default: True)

  • islog (bool) – display log (default: False)

torchcs.dictionary.dfts.odftndict(dictshape, axis=- 1, isnorm=True, islog=False)

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: True)

  • islog (bool) – display log info (default: False)

Returns

OD – Overcomplete nD-DFT dictionary

Return type

torch tensor

torchcs.dictionary.dictshow module

torchcs.dictionary.dictshow.dictshow(D, rcsize=None, stride=None, plot=True, bgcolorv=0, cmap=None, title=None, xlabel=None, ylabel=None)

Trys to show image blocks in one image.

Parameters
  • D (array_like) – Blocks to be shown, a bH-bW-bC-bN numpy ndarray.

  • rcsize (int, tuple or None, optional) – Specifies how many rows and cols of blocks that you want to show, e.g. (rows, cols). If not given, rcsize=(rows, clos) will be computed automaticly.

  • stride (int, tuple or None, optional) – The step size (blank pixels nums) in row and col between two blocks. If not given, stride=(1,1).

  • plot (bool, optional) – True for ploting, False for silent and returns a H-W-C numpy ndarray for showing.

  • bgcolorv (float or None, optional) – The background color, 1 for white, 0 for black. Default, 0.

Returns

out – A H-W-C numpy ndarray for showing.

Return type

ndarray or bool

See also

odctdict.

Examples

>>> D = pys.odctdict((M, N))
>>> showdict(D, bgcolor='k')

torchcs.dictionary.dwts module

torchcs.dictionary.dwts.dwt()

Module contents