- Index
- » Programming
- » Modelica Language
- » want to implement linspace
want to implement linspace
want to implement linspace
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I want to impliment linspace but got an error like above:
[1] 15:38:31 Translation Error
[IFphasemodel_3: 130:1-130:80]: Type mismatch for positional argument 3 in linspace(n=n). The argument has type:
Real
expected type:
Integer
[2] 15:38:31 Translation Error
Error occurred while flattening model IFphasemodel_3
Please help me to rectify it and let me know if you found any other wrong argument as I am converting a Matlab program into openmodelica.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
model IFphasemodel_3
input Real c = 3e8 "speed of light";
parameter Integer[2] param_p = {0,0} "position of radar";
parameter Integer param_N_ant_rx = 4;
input Integer SNR = 0 "rx snr [dB]";
Real[2,2] objects_p_0 = {{0,20},{0,10}} "object's initial (cartesian) positions";
input Real[2,2] position_0 = transpose(objects_p_0);
Real[2,2] objects_v = {{0,10}, {0,0}} "object's initial velocities in x and y directions in [m/s]";
input Real[2,2] velocity_0 = transpose(objects_v);
input Integer[1] objects_rcs = {10} "object's RCS in dBm^2";
input Integer[2] objects_alpha = {1,1} "amplitude of object, in this simple model, no radar equation is used";
input Integer objects_M = size(objects_alpha, 1);
input Real sig_B_sw = 1e9 "sweep BW, [Hz]";
input Real sig_f_0 = 77e9 "start frequency [Hz]";
input Real sig_T_sw = 24e-6 "sweep time (of one sweep/ramp) [s]";
input Integer sig_N_sw = 128 "number of sweeps/ramps";
input Real sig_N_samp_per_ramp = 1024.0 "number of fast-time samples (ADC samples per ramp)";
input Real sig_f_s = 25e6 "sampling frequency";
input Real x1 = 0;
input Real x2 = 1;
input Real n = 0.5.*sig_N_samp_per_ramp;
output Integer objects_d_0_true;
output Integer objects_v_true_abs;
output Integer p_diff;
output Integer p_u;
output Integer objects_v_r_true;
output Integer objects_AoA;
output Integer cos_phi;
output Integer sig_T_s;
output Integer sig_k;
output Integer sig_lambda;
output Integer param_ant_rx_spacing;
output Integer sig_t_sw;
output Integer sig_IF;
output Integer delta_d;
output Integer T_sw;
output Integer T_sw_cont;
output Integer sig_temp;
output Integer d_0_i_m;
output Integer tau_0_i_m;
output Integer Tau;
output Integer phi_m;
output Integer sig_power;
output Integer noise_power;
output Integer sig_noise_temp;
output Integer limits_d_max;
output Integer limits_d_res;
output Integer limits_v_max;
output Integer limits_v_res;
output Integer d_vec;
output Integer v_vec;
output Integer win_range;
output Integer sig_IF_win;
output Integer S_range;
output Integer win_doppler;
output Integer S_range_win;
output Integer S_RD;
equation
sig_T_s = 1/sig_f_s "sampling time";
sig_k = sig_B_sw/sig_T_sw "sweep slope [Hz/s]";
sig_lambda = c/(sig_f_0+sig_B_sw/2);
param_ant_rx_spacing = c/sig_f_0/2 "RX antenna spacing [m]";
sig_t_sw = 0:sig_T_s:(sig_N_samp_per_ramp-1)*sig_T_s;
sig_t_sw = sig_t_sw[:];
objects_d_0_true = zeros(objects_M,1) "convert object parameters to channel parameters - d + v + AoA";
objects_AoA = zeros(objects_M,1);
objects_v_r_true = zeros(objects_M, 1);
for m in 1:objects_M loop
objects_d_0_true(m) = norm(param_p-position_0[:,m]) "distance";
objects_v_true_abs(m) = norm(velocity_0[:, m]) "absolute velocity";
p_diff = position_0[:,m]-param_p "vector from radar to object";
p_u = -p_diff/norm(p_diff) "unit vector from object to radar";
when(objects_v_true_abs(m) > eps) then
cos_phi = transpose(velocity_0[:,m])* p_u/ objects_v_true_abs(m);
objects_v_r_true(m) = objects_v_true_abs(m)*cos_phi "radial velocity";
end when;
objects_AoA(m) = atan2(p_diff(2), p_diff(1))-pi/2 "angle of arrival";
end for;
equation
T_sw_cont = T_sw + sig_T_sw*repmat(0:(sig_N_sw-1), sig_N_samp_per_ramp, 1) "continued time matrix - continuous also along sweep dimension";
delta_d = (param_ant_rx_spacing .* sin(objects_AoA)) "distance difference of arrival at RX antennas";
T_sw = sig_t_sw*ones(1,sig_N_sw) "base time matrix - same for all sweeps";
T_sw_cont = T_sw + sig_T_sw*repmat(0:(sig_N_sw-1), sig_N_samp_per_ramp, 1) "continued time matrix - continuous also along sweep dimension";
for i_ant in 1:param_N_ant_rx loop
sig_temp = zeros([sig_N_samp_per_ramp,sig_N_sw]) "temporary to sum up object signals";
for m in 1:objects_M loop
d_0_i_m = objects_d_0_true(m) "current distance of i-th antenna";
tau_0_i_m = (2*d_0_i_m + delta_d(m).*(i_ant-1))/c "RTDT of i-th antenna - start of first sweep";
Tau = tau_0_i_m + 2*objects_v_r_true(m)/c .* T_sw_cont "compute phase of m-th IF signal";
phi_m = 2*pi*sig_k.*Tau.*T_sw + 2*pi.*Tau.*sig_f_0 - pi .*sig_k.*Tau.^2;
sig_temp = sig_temp + objects_alpha(m)*cos(phi_m);
end for;
sig_power = 10*log10(sum(abs(sig_temp[:].^2))./ (sig_temp[:])) "calculate signal and noise power and generate noise";
noise_power = sig_power-SNR;
sig_noise_temp = sqrt(10^(noise_power/10))*randn(size(sig_temp));
sig_IF[:,:,i_ant] = sig_temp + sig_noise_temp "total IF signal";
end for;
equation
limits_d_max = c*sig_f_s/4/abs(sig_k) "resolution and basis vectors";
limits_d_res = c/2/abs(sig_B_sw);
limits_v_max = c/(4*sig_f_0*sig_T_sw);
limits_v_res = c/(2*sig_f_0*sig_T_sw*sig_N_sw);
d_vec = linspace(x1, x2, n).*limits_d_max "distance vector for first stage DFT";
v_vec = transpose({-sig_N_sw/2 : sig_N_sw/2-1})/sig_N_sw.* 2*limits_v_max "velocity vector";
win_range = fill(hann(sig_N_samp_per_ramp), 1, sig_N_sw, param_N_ant_rx) "Range/Doppler processing - windowing in fast-time";
sig_IF_win = sig_IF* win_range;
S_range = 1/sqrt(sig_N_samp_per_ramp) * realFFT(sig_IF_win, sig_N_samp_per_ramp)"Range/Doppler processing - FFT over fast-time";
S_range = S_range[1:length(d_vec), :, :] "Range/Doppler processing - cut out positive side of spectrum";
win_doppler = transpose(fill(hann(sig_N_sw)), length(d_vec), 1, param_N_ant_rx) "windowing in slow-time/ramps";
S_range_win = S_range.*win_doppler;
S_RD = 1/sqrt(sig_N_sw) * realFFTsamplePoints(S_range_win, sig_N_sw, 2) "FFT over slow-time/ramps";
end IFphasemodel_3;
Re: want to implement linspace
Your variable n that you use as the last argument to linspace is a Real variable, but the last argument to linspace should be an Integer (since it represents the number of elements in the created array).
- perost
- 114 Posts
Re: want to implement linspace
as my value of n=512, I tried with
1) d_vec = linspace(x1, x2, n).*limits_d_max; ..................(with defining input n=512)
2) d_vec = linspace(x1, x2, 512).*limits_d_max;
3) d_vec = linspace(x1, x2, sqrt(22.62)).*limits_d_max;
still the error is same
Re: want to implement linspace
bele wrote:
as my value of n=512, I tried with
1) d_vec = linspace(x1, x2, n).*limits_d_max; ..................(with defining input n=512)
2) d_vec = linspace(x1, x2, 512).*limits_d_max;
3) d_vec = linspace(x1, x2, sqrt(22.62)).*limits_d_max;
still the error is same
Do you really get the same error with variant 2? 3 is of course not valid since sqrt returns a Real value, and 1 is still not valid if you haven't changed the declaration of n to be an Integer.
Here's a small example model that works:
Code:
model M
Real x1 = 0;
Real x2 = 1;
Integer n = 512;
Real vec[512];
equation
vec = linspace(x1, x2, n);
end M;
- perost
- 114 Posts
Re: want to implement linspace
Integer x1 = 0;
Integer x2 = 1;
Integer n = 512;
V = linspace(x1, x2, n);
d_vec = V.*limits_d_max;
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[1] 15:27:58 Translation Error
[IFphasemodel_3: 129:1-129:22]: Type mismatch in equation V=array(/*Real*/(x1) + (/*Real*/(x2) - /*Real*/(x1)) * /*Real*/(-1 + i) / /*Real*/(-1 + n) for i in 1:n) of type Real=Real[n].
[2] 15:27:58 Translation Error
Error occurred while flattening model IFphasemodel_3
Re: want to implement linspace
Well, the error says exactly what the issue is. The left hand side of the equation (V) is a Real, while the right hand side (expanded linspace expression) has type Real[n] (since linspace returns an array with n elements). I.e. you need to declare V to be an array if you want to use it in that way.
Note that you can't use n as the dimension of V, since dimensions must be parameter expressions. But you should probably declare n as a parameter anyway, since it shouldn't change during the simulation. Then you'll also have to change any variables that n depends on to be parameters too, which again is probably a good idea anyway.
Also "d_vec = V.*limits_d_max;" won't work with the code in your first post, since d_vec is neither Real nor an array. This is also true for many of your other variables, which are declared as scalars but used as arrays.
- perost
- 114 Posts
- Index
- » Programming
- » Modelica Language
- » want to implement linspace