.mat 文件和一维数组的卷积



我的代码是:

import numpy as np
import scipy.io as spio
x=np.zeros((22113,1),float)
x= spio.loadmat('C:\Users\dell\Desktop\Rabia Ahmad spring 2016\'
                'FYP\1. Matlab Work\record work\kk.mat')
print(x)
x = np.reshape(len(x),1);
h = np.array([0.9,0.3,0.1],float)
print(h)
h = h.reshape(len(h),1);
dd = np.convolve(h,x)

我遇到的错误是"值错误:对象对于所需的数组来说太深"请帮助我进行这次守卫。

{'__globals__': [], '__version__': '1.0', 'ans': array([[ 0.13580322,
 0.13580322], [ 0.13638306, 0.13638306], [ 0.13345337, 0.13345337], 
 ..., [ 0.13638306, 0.13638306], [ 0.13345337, 0.13345337], ..., [   
 0.13638306, 0.13638306], [ 0.13345337, 0.13345337], ..., [-0.09136963,    
-0.09136963], [-0.12442017, -0.12442017], [-0.15542603, -0.15542603]])}

看到{}? 这意味着loadmat x是字典。

x['ans']将是一个数组

array([[ 0.13580322,
     0.13580322], [ 0.13638306, 0.13638306], [ 0.13345337, 0.13345337],...]])

如果我计算 [] 右边是一个 (n,2) 浮点数数组。

以下行没有意义:

x = np.reshape(len(x),1);

我怀疑你的意思是x = x.reshape(...)就像你对h一样. 但这会使字典x出错。

当你说the shape of x is (9,) and its dtype is uint16 - 你在代码中的哪个地方验证它?

x = np.reshape(len(x),1);没有

做任何有用的事情。这完全丢弃了x中的数据,并创建了一个形状(1,)数组,唯一的元素是len(x)

在您的代码中,您将h重塑为 (3, 1) ,这是一个 2D 数组,而不是 1D 数组,这就是convolve抱怨的原因。

删除你的两个reshape,而只是将squeeze=True传递给scipy.io.loadmat - 这是必需的,因为 matlab 没有 1D 数组的概念,squeeze 告诉 Scipy 尝试展平(N, 1)并将数组(1, N) (N,)数组

最新更新