python 实现maltab中离散正弦变换dst及其反变换idst

DFT、DCT、DST数学公式及原理部分可参考该博客
笔者在将某matlab程序代码重新用python实现过程中,发现scipy.fft.dst的运行结果和matlab中的dst函数运行结果不一致(如下图所示)。前者主要有type 和axis两个参数,改变参数也无法得到和matlab中相同结果,便用python重写了matlab中的dst函数及其反变换idst函数。欢迎大家批评指正!
在这里插入图片描述
在这里插入图片描述

  • matlab 中dst 源码
function b=dst(a,n)
%DST    Discrete sine transform.
%
%       Y = DST(X) returns the discrete sine transform of X.
%       The vector Y is the same size as X and contains the
%       discrete sine transform coefficients.
%
%       Y = DST(X,N) pads or truncates the vector X to length N
%       before transforming.
%
%       If X is a matrix, the DST operation is applied to each
%       column. This transform can be inverted using IDST.
%
%       See also: FFT, IFFT, IDST.

%       A. Nordmark 10-25-94.
%       Copyright 1994-2016 The MathWorks, Inc.


narginchk(1,2);

if min(size(a))==1
    if size(a,2)>1
        do_trans = 1;
    else
        do_trans = 0;
    end
    a = a(:);
else
    do_trans = 0;
end
if nargin==1
  n = size(a,1);
end
m = size(a,2);

% Pad or truncate a if necessary
if size(a,1)<n
  aa = zeros(n,m);
  aa(1:size(a,1),:) = a;
else
  aa = a(1:n,:);
end

y=zeros(2*(n+1),m);
y(2:n+1,:)=aa;
y(n+3:2*(n+1),:)=-flipud(aa);
yy=fft(y);
b=yy(2:n+1,:)/(-2*sqrt(-1));

if isreal(a), b = real(b); end
if do_trans, b = b.'; end

% LocalWords:  Nordmark
%%%%%%%%%%%%%%%%%%%%%%%%%%%
function b=idst(a,n)
%IDST   Inverse discrete sine transform.
%
%       X = IDST(Y) inverts the DST transform, returning the
%       original vector if Y was obtained using Y = DST(X).
%
%       X = IDST(Y,N) pads or truncates the vector Y to length N
%       before transforming.
%
%       If Y is a matrix, the IDST operation is applied to
%       each column.
%
%       See also: FFT, IFFT, DST.

%       A. Nordmark 10-25-94.
%       Copyright 1994-2001 The MathWorks, Inc.

if nargin==1
  if min(size(a))==1
    n=length(a);
  else
    n=size(a,1);
  end
end

nn=n+1;
b=2/nn*dst(a,n);

示例测试结果如下:
在这里插入图片描述

  • dst的python实现
def DST(a,*args):
    shape = a.shape
    if len(shape) == 1:
        if shape[0] > 1:
            do_trans = 1
        else:
            do_trans = 0
        a = np.ravel(a)
        a = a.reshape(-1,1)
    else:
        do_trans = 0

    shape = a.shape

    if args:
        n = args[0]
    else:
        n = shape[0]

    m = a.shape[1]

    if shape[0] < n:
        aa = np.zeros((n,m))
        aa[0:shape[0],:] = a
    else:
        aa = a[0:n,:]

    y = np.zeros((2 * (n + 1),m))
    y[1:n+1,:] = aa

    y[n+2:2*(n+1),:] = -np.flipud(aa)
    yy = np.fft.fft(y,axis=0)

    b = yy[1:n+1,:] / (-2j)

    if np.isreal(a.any()):
        b = np.real(b)
    if do_trans:
        b = b.T

    return b

def IDST(a,*args):
    if args:
        n = args[0]
    else:
        n = a.shape[0]

    nn = n + 1
    b = 2/nn * DST(a,n)
    return b

运行结果如下:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
离散余弦变换(DCT)和离散正弦变换DST)是数字信号处理常用的一种变换方法。它们可以将一维或二维的离散信号转换为一组系数,这些系数可以用于信号压缩、特征提取、信号恢复等应用。 离散余弦变换(DCT)是一种将时域信号转换为频域信号的技术,它将信号表示为一组余弦函数的线性组合。DCT广泛应用于图像和音频压缩领域,其JPEG图像压缩和MP3音频压缩都使用了DCT。 离散正弦变换DST)是一种将时域信号转换为频域信号的技术,它将信号表示为一组正弦函数的线性组合。DST在信号处理也有广泛的应用,例如在图像处理DST可以用于图像的边缘检测和纹理分析。 以下是两个Python实现DCT和DST的例子: 1.使用SciPy库的DCT和DST函数 ```python import numpy as np from scipy.fftpack import dct, idct, dst, idst # 生成一个长度为8的随机信号 x = np.random.rand(8) # 计算DCT和IDCT dct_x = dct(x, type=2) idct_x = idct(dct_x, type=2) # 计算DST和IDST dst_x = dst(x, type=2) idst_x = idst(dst_x, type=2) print("原始信号:", x) print("DCT变换后的信号:", dct_x) print("IDCT变换后的信号:", idct_x) print("DST变换后的信号:", dst_x) print("IDST变换后的信号:", idst_x) ``` 2.使用PyWavelets库的DCT和DST函数 ```python import numpy as np import pywt # 生成一个长度为8的随机信号 x = np.random.rand(8) # 计算DCT和IDCT dct_x = pywt.dct(x, 'dct', norm='ortho') idct_x = pywt.idct(dct_x, 'dct', norm='ortho') # 计算DST和IDST dst_x = pywt.dwt(x, 'db1', 'd', norm='ortho')[0] idst_x = pywt.idwt(dst_x, np.zeros_like(dst_x), 'db1', 'd', norm='ortho') print("原始信号:", x) print("DCT变换后的信号:", dct_x) print("IDCT变换后的信号:", idct_x) print("DST变换后的信号:", dst_x) print("IDST变换后的信号:", idst_x) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值