2012-11-14 32 views
1

我轉換函數從MATLAB到C++,需要一個矢量並分離到含有600個值並在200。這裏的時間間隔改變的塊是函數(在Matlab):將矢量拆分成塊 - 這是正確的嗎?

function f = block(v, N, M) 

% This function separates the vector 
% into blocks. Each block has size N. 
% and consecutive blocks differ in 
% their starting positions by M 
% 
% Typically 
% N = 30 msec (600 samples) 
% M = 10 msec (200 samples) 

n = length(v); 
maxblockstart = n - N + 1; 
lastblockstart = maxblockstart - mod(maxblockstart-1 , M); 

% Remove the semicolon to see the number of blocks 
% numblocks = (lastblockstart-1)/M + 1 
numblocks = (lastblockstart-1)/M + 1; 

f = zeros(numblocks,N); 

for i = 1:numblocks 
for j = 1:N 
    f(i,j) = v((i-1)*M+j); 
end 

而在C++函數:

vector<iniMatrix> Audio::something(vector<double>& theData, int N, int M) 
{ 
//How many blocks of size N can we get? 
int num_blocks = theData.size()/N; 

int n = theData.size(); 
int maxblockstart = n - N; 
int lastblockstart = maxblockstart - (maxblockstart % M); 
int numbblocks = (lastblockstart)/M + 1; 

this->width = N; 

//Create the vector with enough empty slots for num_blocks blocks 
vector<iniMatrix> blocked(num_blocks); 

//Loop over all the blocks 
for(int i = 0; i < num_blocks; i++) { 
    //Resize the inner vector to fit this block    
    blocked[i].resize(N); 

    //Pull each sample for this block 
    for(int j = 0; j < N; j++) { 
     blocked[i][j] = theData[i*N+j]; 
    } 
} 
return blocked; 

}

這是否看起來是正確的?我知道這是一個奇怪的問題,我可能會得到負面反饋,但是,我試着計算過零點的數量,並顯示錯誤的答案,所以,我如何分解矢量可能有問題。

編輯:

iniMatrix是2D矢量的一個typedef

可生產96塊(含600個值)

+1

blocked [i] [j] = theData [i * N + j]; N是錯字? –

+0

@DanilAsotsky你是什麼意思? 「blocked [i] [j] = theData [i * N + j];」是不是一個錯字..有沒有錯誤? – Phorce

+0

你在Matlab代碼(代碼中的最後一個循環)中有v((i-1)* M + j),但是在C++代碼中它是數據[i * N + j]。似乎有誤印,可能導致錯誤。例如,你需要有數據[i * M + j]。 –

回答

1

您在Matlab代碼的最後一個循環具有

f(i,j) = v((i-1)*M+j); 

,但在C++代碼中是

blocked[i][j] = theData[i*N+j]; 

看起來有誤印,可能導致錯誤。例如,你需要用C++編寫代碼:

blocked[i][j] = theData[i*M+j];