Matlab与Python:切片

Matlab与Python:切片,python,matlab,numpy,Python,Matlab,Numpy,我正在尝试将一些Matlab代码转换为Python。我在切片方面有问题 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

我正在尝试将一些Matlab代码转换为Python。我在切片方面有问题

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代码:

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))

我只想问,是
demodm_3_b=y2(sa/2:sa:end)和Python中的
demodm_3_v=y2[(sa/2)-1:sa:,]
相同?

是的,您的索引错误。在NumPy中,以下内容适用:

基本的切片语法是i:j:k,其中i是开始索引,j是停止索引,k是步骤(k)≠0)

因此,您在Python中需要的是:

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

与Matlab不同,步长是最后一个输入。当您要处理整个数组长度时,请不要在这两个数组之间放置任何内容。

对不起,不能发布整个代码。它非常大。您到底想做什么?你能把代码精简成一个简短的、自包含的、可运行的代码片段来演示这个问题吗?thaks这确实很有帮助