2013-07-30 41 views
0

我想將一些Matlab代碼轉化爲Python。我遇到切片問題。Matlab vs 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)) 

我只想問,在Matlab demod_3_b=y2(sa/2:sa:end);demod_3_v=y2[(sa/2)-1:sa:,]在Python一樣嗎?

+0

抱歉無法發佈整個代碼非常巨大 –

+3

你甚至想要做什麼?您是否可以將代碼修剪爲一個簡短的,自包含的,可運行的代碼片段,以顯示問題? – user2357112

回答

2

是的,你的索引是錯誤的。在NumPy的,以下indexing適用:

基本切片語法是I:Y:k,其中i是起始索引,j是停止索引,而k是步驟(k≠0)。

因此,你所使用Python尋找的是:

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

與Matlab中,步長是最後一個輸入。當你想處理你的陣列的整個長度時,不要在兩個:之間放置任何東西。

+0

這確實是有幫助的 –