简体   繁体   English

Matlab与Python:切片

[英]Matlab Vs Python: Slicing

I am trying to covert some Matlab code to Python. 我试图将一些Matlab代码转换为Python。 I am having problems with slicing. 我有切片问题。

Matlab Code: Matlab代码:

demod_1_b=-1*mod_noisy*2.*sin(2*pi*Fc*t+phi);
y = filter(Hd,demod_1_b);
y2=conv(y,raised)/ConvFac;
%% till this line the length in python and Matlab are same
y2=y2(sa/2:end-(sa/2));
%%%% when i write this line in Python it gives me wrong answer it should come out as   26     but in python it gives me 33 i think i havnt converted it in a rigth way 
demod_3_b=y2(sa/2:sa:end);

Python Code: Python代码:

demod_1_b=-1*mod_noisy*2*sin((2*pi*Fc*t)+phi)

N=10
Fc=40
Fs=1600
d=firwin(numtaps=N,cutoff=40,nyq=Fs/2)
print(len(d))
Hd=lfilter( d, 1.0, demod_1_b)
y2=(convolve(Hd,raised))/Convfac
print(len(y2))
y2=y2[(sa/2)-1:-sa/2]
print(len(y2))
 # problem starts here
demod_3_b=y2[(sa/2)-1:sa:,]
print(len(demod_3_a))

I just want ask, are demod_3_b=y2(sa/2:sa:end); 我只想问,是demod_3_b=y2(sa/2:sa:end); in Matlab and demod_3_v=y2[(sa/2)-1:sa:,] in Python the same? 在Matlab和demod_3_v=y2[(sa/2)-1:sa:,]在Python中是一样的吗?

Yes, your indexing is wrong. 是的,您的索引是错误的。 In NumPy, the following indexing applies: 在NumPy中,以下索引适用:

The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (k≠0). 基本切片语法是i:j:k其中i是起始索引,j是停止索引,k是步骤(k≠0)。

Hence, what you are looking for in Python is: 因此,您在Python中寻找的是:

y2[(sa/2)-1::sa]

Unlike in Matlab, the stepsize is the last input. 与Matlab不同,stepsize是最后一个输入。 As you want to process the entire length of your array, don't put anything between the two : . 当你想要处理数组的整个长度时,不要在两者之间放置任何东西:

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

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