Reputation: 1891
I'm very beginner in Matlab and can't convert the following code to c++
suppose that conv is already written in C++, I don't know if the code is implementing a 2D conv or 1D actually
How would I convert the following code to C++
% Description:
% Function that evaluates the equivalent loop transfer function from the
% loop integrator gain
%
% Input:
% KK [real,vector]: Integrator gains
% N [integer] : Order of the loop filter
% Output:
% A [real, vector]: Coefficients of the denominator
% B [real, vector]: Coefficients of the numerator
%
% Remarks:
% o A rate-only NCO is assumed
% construct the loop filter transfer function
v1 = 1;
v2 = 1;
B = zeros(1, N);
for ii = N:-1:1,
v1 = conv( v1, [1 -1] );
B = B + KK(ii)*[v2 zeros(1, ii - 1)];
v2 = conv(v2, [1 -1]);
end
v1 = conv( v1, [1 0]);
% Account for the rate-only NCO - Change this code to implement different
% NCO models
B = conv(B, [1 1])*1/2;
B = [ 0 B ];
A = v1 + B;
Upvotes: 0
Views: 58
Reputation: 2495
#include <vector>
#include <algorithm>
using std::vector;
int main() {
// v1 = 1;
double v1 = 1;
// v2 = 1;
double v2 = 1;
// B = zeros(1, N);
vector<double> B(N, 0);
//for ii = N:-1:1,
for (int ii=N; ii>=1; ii--) {
// v1 = conv( v1, [1 -1] );
v1 = conv(v1, {1, -1});
// B = B + KK(ii)*[v2 zeros(1, ii - 1)];
vector<double> temp(v2.size() + ii - 1, 0); // initialize with zeros
std::copy(v2.begin(), v2.end(), temp.begin()); // copy v2 to temp
B += temp * KK[ii];
// v2 = conv(v2, [1 -1]);
v2 = conv(v2, {1, -1});
// end
}
// v1 = conv( v1, [1 0]);
v1 = conv(v1, {1, 0});
// B = conv(B, [1 1])*1/2;
B = conv(B, {1, 1}) * 0.5;
// B = [ 0 B ];
B.insert(B.begin(), 0);
// A = v1 + B;
vector<double> A = v1 + B;
}
Upvotes: 1