本文介绍了Scipy:生成 NxN 离散余弦矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 scipy,是否有一种简单的方法可以模拟 MATLAB 的 dctmtx 函数的行为,该函数为某些给定的 N 返回一个 NxN DCT 矩阵?有 scipy.fftpack.dctn 但这仅适用于 DCT.如果我不想使用除 scipy 之外的其他依赖项,我是否必须从头开始实现?

Using scipy, is there an easy way to emulate the behaviour of MATLAB's dctmtx function which returns a NxN DCT matrix for some given N? There's scipy.fftpack.dctn but that only applies the DCT.Do I have to implement this from scratch if I don't want use another dependency besides scipy?

推荐答案

DCT 是一种线性变换,因此获得变换矩阵的一种方法是将其应用于单位矩阵.这是我找到长度为 8 的序列的矩阵的示例(对于一般情况,将 8 更改为 N):

The DCT is a linear transformation, so one way to get the matrix for the transformation is to apply it to the identity matrix. Here's an example where I find the matrix for sequences of length 8 (change 8 to N for the general case):

In [124]: import numpy as np

In [125]: from scipy.fft import dct

In [126]: D = dct(np.eye(8), axis=0)

D 是矩阵:

In [127]: D
Out[127]:
array([[ 2.        ,  2.        ,  2.        ,  2.        ,  2.        ,  2.        ,  2.        ,  2.        ],
       [ 1.96157056,  1.66293922,  1.11114047,  0.39018064, -0.39018064, -1.11114047, -1.66293922, -1.96157056],
       [ 1.84775907,  0.76536686, -0.76536686, -1.84775907, -1.84775907, -0.76536686,  0.76536686,  1.84775907],
       [ 1.66293922, -0.39018064, -1.96157056, -1.11114047,  1.11114047,  1.96157056,  0.39018064, -1.66293922],
       [ 1.41421356, -1.41421356, -1.41421356,  1.41421356,  1.41421356, -1.41421356, -1.41421356,  1.41421356],
       [ 1.11114047, -1.96157056,  0.39018064,  1.66293922, -1.66293922, -0.39018064,  1.96157056, -1.11114047],
       [ 0.76536686, -1.84775907,  1.84775907, -0.76536686, -0.76536686,  1.84775907, -1.84775907,  0.76536686],
       [ 0.39018064, -1.11114047,  1.66293922, -1.96157056,  1.96157056, -1.66293922,  1.11114047, -0.39018064]])

验证 D @ x 是否等价于 dct(x):

Verify that D @ x is equivalent to dct(x):

In [128]: x = np.array([1, 2, 0, -1, 3, 0, 1, -1])

In [129]: dct(x)
Out[129]: array([10.        ,  4.02535777, -1.39941754,  7.38025967, -1.41421356, -6.39104653, -7.07401092,  7.51550307])

In [130]: D @ x
Out[130]: array([10.        ,  4.02535777, -1.39941754,  7.38025967, -1.41421356, -6.39104653, -7.07401092,  7.51550307])

请注意,D @ x 通常会比 dct(x) 慢得多.

Note that D @ x will generally be much slower than dct(x).

要与 Matlab 的 dctmtx,添加参数 norm='ortho'.例如,这里是 Octave 中的 dctmtx(它返回与 Matlab 中相同的数组):

To get exact agreement with Matlab's dctmtx, add the argument norm='ortho'. For example, here's dctmtx in Octave (which returns the same array as in Matlab):

octave:1> pkg load image
octave:2> dctmtx(4)
ans =

   0.50000   0.50000   0.50000   0.50000
   0.65328   0.27060  -0.27060  -0.65328
   0.50000  -0.50000  -0.50000   0.50000
   0.27060  -0.65328   0.65328  -0.27060

以下是使用 scipy.fft.dct 的计算:

Here's the calculation using scipy.fft.dct:

In [56]: from scipy.fft import dct

In [57]: dct(np.eye(4), axis=0, norm='ortho')
Out[57]:
array([[ 0.5       ,  0.5       ,  0.5       ,  0.5       ],
       [ 0.65328148,  0.27059805, -0.27059805, -0.65328148],
       [ 0.5       , -0.5       , -0.5       ,  0.5       ],
       [ 0.27059805, -0.65328148,  0.65328148, -0.27059805]])

这篇关于Scipy:生成 NxN 离散余弦矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 01:53