简体   繁体   English

无法将MATLAB转换为repmat和对称性的Python代码

[英]Unable to convert MATLAB to Python code for repmat and symmetry

MATLAB code: MATLAB代码:

n = 2048;
d = 1;
order = 2048;
nn = [-(n/2):(n/2-1)]';
h = zeros(size(nn),'single');
h(n/2+1) = 1 / 4;
odd = mod(nn,2) == 1;
h(odd) = -1 ./ (pi * nn(odd)).^2;
f_kernel = abs(fft(h))*2;
filt = f_kernel(1:order/2+1)';
w = 2*pi*(0:size(filt,2)-1)/order;
filt(w>pi*d) = 0;                     
filt = [filt , filt(end-1:-1:2)]; 
filt = repmat(filt',[1 1024]);

Python Code : Python代码:

import numpy as np
import numpy.matlib
from numpy.matlib import repmat
d = 1
filt_length = 2048
nn = np.linspace(-1024,1023,2048)
nn = np.transpose(nn)
h = np.zeros((2048))
h[1024] = 0.25
odd = (nn%2)
for i in range(0,2048) :
    if odd[i] == 1 :
    h[i] = -1/((np.pi*nn[i])**2)       

f_kernel = abs(fft(h))*2
filt = np.transpose(f_kernel[0:1024])
w = (np.pi)*np.linspace(0,1,1025)

However I have been unable to convert the last 3 lines of the MATLAB code to Python. 但是,我无法将MATLAB代码的最后3行转换为Python。 Any suggestions? 有什么建议么? The second last step of MATLAB code creates a ramp filter of size 2048 (goes from 0 to 1 in steps of 1024 and 1 to 0 in another 1024 steps). MATLAB代码的倒数第二步创建一个大小为2048的渐变滤波器(在1024步中从0到1,在另外1024步中从1到0)。 The last repmat makes the size of filt as (2048,1024). 最后一个repmat使filt的大小为(2048,1024)。

If I were on my computer, I'd start Octave and IPython sessions, and start replicating the code line by line. 如果我在计算机上,则将启动Octave和IPython会话,并开始逐行复制代码。 I'd use smaller dimensions to easily watch the results. 我会使用较小的尺寸轻松查看结果。 I'd pay special attention to shapes. 我会特别注意形状。 Since my Matlab is rusty it is easier to do it this way than in my head. 因为我的Matlab生锈了,所以用这种方法比在脑海中容易做。 And more reliable. 而且更可靠。

np.arange is easier to use than linspace . np.arangelinspace更易于使用。 transpose is unnecessary with 1d arrays. transpose维数组不需要transpose np.repeat or np.tile will probably perform repmat's job. np.repeatnp.tile可能会执行repmat's工作。 I don't recall exactly what repmat does. 我不记得repmat是做什么的。 Initially I would copy loops, but I'd try replace them when code is working. 最初,我会复制循环,但是在代码正常工作时,我会尝试替换它们。 fft may require scipy . fft可能需要scipy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM