How can I generate PWM in MatLab - matlab-figure

I want to generate a PWM signal. I have not achieved the expected result.
close all
clear all
fs=1000;
ts=1/fs;
t=[-5:ts:5];
fc =10; %Frecuencia señal portadora
fm = 1; %Frecuencia señal del mensaje
a = 5; %Amplitud de la señal portadora
b = 0.25; %Amplitud de la señal del mensaje
vm = b.*sin(2*pi*fm*t); %Genera la Señal del mensaje
bits = [1, 0, 1, 1];
amp = (2*bits-1);
vc = rect(t/b) + rect((t+1)/b) ...
+ rect((t+2)/b) + rect((t+3)/b)+ rect((t+4)/b)+ rect((t+5)/b)+...
rect((t-1)/b) ...
+ rect((t-2)/b) + rect((t-3)/b)+ rect((t-4)/b)+ rect((t-5)/b)
n = length(vc);
for i = 1:n
if (vm(i)>=vc(i))
pwm(i) = 1;
else
pwm(i) = 0;
end
end
I don't get the real PWM signal. Help me.

Related

Undefined Variable "xgetfile" in SciLab

[\n ENTER SAMPLE SIZE:28428
\n ENTER TOLERANCE LIMIT:100
Undefined variable: xgetfile]1
So basically when I run this program that I have, downloaded from the internet, it always tells me that the variable xgetfile is undefined rather than prompting me to select a file with the data in it. The full code for said program is pasted down below. My question is how to remedy this issue and be able to input my data. The line that says xgetfile is pretty near the top so you don't have to do too much reading to get to it.
n=0;
tol_lim=0;
// ENTERING SAMPLE SIZE
while n<=5 | n==[] , n=input("\n ENTER SAMPLE SIZE:");
if (n<=5) printf("\n\n SAMPLE SIZE SHOULD BE GREATER THAN 5\n\n");end
if n==[] printf("\n\n SAMPLE SIZE CANNOT BE LEFT BLANK\n\n");end
end
//ENTERING TOLERANCE LIMIT
while tol_lim <= 0 | tol_lim==[], tol_lim=input("\n ENTER TOLERANCE LIMIT:");
if (tol_lim<=0) printf("\n TOLERANCE LIMIT SHOULD BE GREATER THAN 0\n\n");end
if tol_lim==[] printf("\n TOLERANCE LIMIT CANNOT BE LEFT BLANK\n\n");end
end
//INITIALIZING VARIABLES
F = zeros(n,3);
Y = zeros(n,1);
OY = zeros(n,1);
EY = zeros(n,1);
DOY = zeros(n,1);
Estimated_Y = zeros(n,1);
d = zeros(3,1);
THETA = zeros(3,1);
GN1=0;GN2=0;GN3=0;
sig=0;y=0;
sigma_hat_square=0; y_bar=0; dff=0; R_square=0;
U_t_hat=0; U_t_hat_square=0; U_t_minusone_hat=0; dd=0; DW=0;Covariance_Matrix=zeros(3,3);
f_obs=0; l_obs=0; r=0; D1=0; D2=0;
S1=0;S2=0;S3=0;D1=0;D2=0;r=0;sum_Y=0;Y_bar=0;Y_square=0;D_den=0;D=0;
AY=zeros(n,1);
OBS=zeros(n,1);
EST=zeros(n,1);
g=[];gh=[];
EXISTING_DATA='';
//CHOOSING INPUT EXCEL DATA FILE
gh=xgetfile();
while gh==[], gh=xgetfile('*.*',title='CHOOSE A FILE NAME');
if g==[] printf("FILE NAME CANNOT BE LEFT BLANK");end
end
Sheets=readxls(gh);
EXISTING_DATA=Sheets(1);
typeof(EXISTING_DATA);
printf("\n\n");
//DISPLAYING EXISTING DATA FROM EXCEL FILE
EXISTING_DATA
for i=2:(n+1),DOY(i-1,1)=EXISTING_DATA(i,2);end
while f_obs<=0 | f_obs==[] , f_obs=input("\n ENTER FIRST OBSERVATION NO:");
//if (f_obs<=0) printf("\n\n IT SHOULD BE GREATER THAN 0\n\n");end
//if f_obs==[] printf("\n\n IT CANNOT BE LEFT BLANK\n\n");end
end
while l_obs<f_obs | l_obs==[] , l_obs=input("\n ENTER LAST OBSERVATION NO:");
//if (l_obs<=f_obs) printf("\n\n IT SHOULD BE GREATER THAN FIRST OBSERVATION NO:\n\n");end
//if l_obs==[] printf("\n\n IT CANNOT BE LEFT BLANK\n\n");end
end
for i=1:n,OY(i,1)=log(DOY(i,1));end
r = ((l_obs - f_obs) + 1)/3;
for i=1:r, S1 = S1 + OY(i,1);end
for i=r+1:2*r, S2 = S2 + OY(i,1);end
for i=2*r+1:3*r, S3 = S3 + OY(i,1);end
D1 = S1 - S2;
D2 = S2 - S3;
A=0;B=0;C=0;
// CALCULATING INITIAL ESTIMATES OF A, B, C
C = (D2/D1)^(1/r);
B = ((1 - C)/C)* [(D1^3)/(D1-D2)^2];
A = (1/3)*(1/r)*[(S1 + S2 + S3) - (D1^2 + D1*D2 + D2^2)/(D1 - D2)];
Ini_A=A; Ini_B=B;Ini_C=C;
for i=1:n, F(i,1)=1;end
for i=1:n, F(i,2)=C^i;end
for i=1:n, F(i,3)=i*B*(C^(i-1));end
for i=1:n, EY(i,1)=A + B*(C^i);end
for i=1:n, Y(i,1) = OY(i,1) - EY(i,1);end
d = inv(F'*F)*F'*Y;
THETA(1,1) = A + d(1,1);
THETA(2,1) = B + d(2,1);
THETA(3,1) = C + d(3,1);
if abs(d(1,1)/A) < tol_lim & abs(d(2,1)/B) < tol_lim & abs(d(3,1)/C) < tol_lim
break;
end
for cnt=1:100
A = THETA(1,1);
B = THETA(2,1);
C = THETA(3,1);
for i=1:n, F(i,1)=1;end
for i=1:n, F(i,2)=C^i;end
for i=1:n, F(i,3)=i*B*(C^(i-1));end
for i=1:n, EY(i,1)=A + B*(C^i);end
for i=1:n, Y(i,1) = OY(i,1) - EY(i,1);end
d = inv(F'*F)*F'*Y;
THETA(1,1) = A + d(1,1);
THETA(2,1) = B + d(2,1);
THETA(3,1) = C + d(3,1);
if abs(d(1,1)/A) < tol_lim & abs(d(2,1)/B) < tol_lim & abs(d(3,1)/C) < tol_lim
break;
end
end
A=THETA(1,1);
B=THETA(2,1);
C=THETA(3,1);
for i=1:n,
GN1 = GN1 + (OY(i,1) - A - B*(C^i));
GN2 = GN2 + (OY(i,1) - A - B*(C^i))*(C^i);
GN3 = GN3 + (OY(i,1) - A - B*(C^i))*B*i*(C^(i-1));
end
p_GN1=GN1;
p_GN2=GN2;
p_GN3=GN3;
for i=1:n, EY(i,1) = A + B*(C^i);end
for i=1:n, Y(i,1) = OY(i,1) - EY(i,1);end
for i=1:n, sig =sig + Y(i,1)*Y(i,1);end
sigma_hat_square = sig/n;
for i=1:n, y = y + OY(i,1);end
y_bar = y/n;
for i=1:n, dff = dff + (OY(i,1) - y_bar)*(OY(i,1) - y_bar);end
R_square = 1 - (sig/dff);
for i=1:n,
F(i,1)=1;
F(i,2)=C^i;
F(i,3)=i*B*(C^(i-1));
end
//Showing Covariance Matrix
Covariance_Matrix = sigma_hat_square*inv(F'*F);
G = zeros(3,3);
G = inv(F'*F);
//Showing Standard Errors
std_err_A = sqrt((sigma_hat_square)*G(1,1));
std_err_B = sqrt((sigma_hat_square)*G(2,2));
std_err_C = sqrt((sigma_hat_square)*G(3,3));
for i=1:n,
U_t_hat_square = U_t_hat_square+ ((OY(i,1) - A - B*(C^i))^2);
end
for i=2:n,
U_t_hat = OY(i,1) - A - B*(C^i);
U_t_minusone_hat = OY(i-1,1) - A - B*(C^(i-1));
dd = dd + (U_t_hat - U_t_minusone_hat)*(U_t_hat - U_t_minusone_hat);
end
DW = dd/U_t_hat_square;
for i=1:n, EY(i,1) = A + B*(C^i);end
for i=1:n, Y(i,1) = OY(i,1) - EY(i,1);end
for i=1:n,sum_Y = sum_Y + OY(i,1);end
Y_bar = sum_Y /n;
for i=1:n, Y_square = Y_square + Y(i,1)*Y(i,1);end
for i=1:n, D_den = D_den + (OY(i,1) - Y_bar)*(OY(i,1) - Y_bar);end
D = Y_square/D_den;
printf("\n\n\nREPORT SHOWING RESULTS\n");
printf("----------------------\n\n\n");
printf("Sample Size = %d Tolerance Limit=%f\n\n",n,tol_lim);
printf("PARAMETER INITIAL ESTIMATES FINAL ESTIMATES STD. ERRORS DW ");
printf("\n------- ----------------- ---------------- ------------ ---- \n");
printf("\nA %f %f %f %f", Ini_A,A, std_err_A, DW);
printf("\nB %f %f %f ", Ini_B,B, std_err_B);
printf("\nC %f %f %f ", Ini_C,C, std_err_C);
printf("\n\n\n ");
printf("No. of Iterations: = %d\n\n",cnt+1);
printf("GN1 = %.7f\n\n", p_GN1);
printf("GN2 = %.7f\n\n", p_GN2);
printf("GN3 = %.7f\n\n", p_GN3);
printf("Sigma_Hat_Square= %f \t\t R_Square= %f\t D=%f",sigma_hat_square, R_square,D);
printf("\n\n COVARIANCE MATRIX \n");
printf("-------------------------\n");
printf("%f\t\t%f\t\t%f\n",Covariance_Matrix(1,1), Covariance_Matrix(1,2), Covariance_Matrix(1,3));
printf("%f\t\t%f\t\t%f\n",Covariance_Matrix(2,1), Covariance_Matrix(2,2), Covariance_Matrix(2,3));
printf("%f\t\t%f\t\t%f",Covariance_Matrix(3,1), Covariance_Matrix(3,2), Covariance_Matrix(3,3));
printf("\n\n Residuals\n\n ");
printf("Y = %f\n",Y);
x=input("\n\n Exit Program??...Press 1 to exit or enter to save");
if (x==1)
exit();
end
g=x_dialog(['enter file name:']);
u=mopen(g,'w');
mfprintf(u,"REPORT SHOWING RESULTS\n");
mfprintf(u,"----------------------\n\n\n");
mfprintf(u,"Sample Size = %d Tolerance Limit = %f\n\n\n",n,tol_lim);
mfprintf(u,"PARAMETER INITIAL ESTIMATES FINAL ESTIMATES STD. ERRORS DW ");
mfprintf(u,"\n------- ----------------- ---------------- ------------ ------ \n");
mfprintf(u,"\nA %f %f %f %f", Ini_A,A, std_err_A, DW);
mfprintf(u,"\nB %f %f %f ", Ini_B,B, std_err_B);
mfprintf(u,"\nC %f %f %f ", Ini_C,C, std_err_C);
mfprintf(u,"\n\n\n ");
mfprintf(u,"No. of Iterations: = %d\n\n",cnt+1);
mfprintf(u,"GN1 = %.7f\n\n", p_GN1);
mfprintf(u, "GN2 = %.7f\n\n", p_GN2);
mfprintf(u, "GN3 = %.7f\n\n", p_GN3);
mfprintf(u, "Sigma_Hat_Square= %f \t\t R_Square= %f\t D=%f",sigma_hat_square, R_square,D);
mfprintf(u,"\n\n COVARIANCE MATRIX \n");
mfprintf(u,"-------------------------\n");
mfprintf(u,"%f\t\t%f\t\t%f\n",Covariance_Matrix(1,1), Covariance_Matrix(1,2), Covariance_Matrix(1,3));
mfprintf(u,"%f\t\t%f\t\t%f\n",Covariance_Matrix(2,1), Covariance_Matrix(2,2), Covariance_Matrix(2,3));
mfprintf(u,"%f\t\t%f\t\t%f",Covariance_Matrix(3,1), Covariance_Matrix(3,2), Covariance_Matrix(3,3));
mfprintf(u,"\n\n SHOWING RESIDUALS\n\n ");
mfprintf(u, "Y = %f\n",Y);
mclose(u);
t=[1:1:n]';
Estimated_Y=A + B*(C^t);
for i=1:n, OBS(i,1)=OY(i,1);end
for i=1:n,EST(i,1)=Estimated_Y(i,1);end
plot2d(t,[OBS,EST],[2,3],leg="Observed#Estimated",nax=[1,n,1,n]);
legends(['t';'(Year)'],[1,1],opt="lr")
legends(['Y';'(Dependent Variable)'],[1,1],opt="ul")
xtitle("GOMPERTZ GROWTH CURVE");
end_prog=input("\n\n Continue??..PRESS 1 TO CONTINUE.....PRESS 2 TO EXIT");
if (end_prog==1)
exec("C:\SCILAB\Gompertz.sce");
end
if (end_prog==2)
printf("CLOSING PROGRAM........");
exit;
end
Your Scilab program seems to be written for a very old version since xgetfile disappeared with version 5.2 (year 2001). Just replace the line with
gh=uigetfile('*.*','CHOOSE A FILE NAME');
and this part of your program will work.

Local infeasibility in MATLAB

I need help with a dynamic optimization problem that consist in a consumed energy optimization of a UAV with this optimal control problem.
enter image description here
enter image description here
My code is this
Ecuations:
Parameters
tf
#Velocidad de rotores rad/s
#Las condiciones iniciales permiten igualar la acción de la gravedad
#Se tomo 4000rad/s como la velocidad maxima de los rotores
w1 = 912.32, >=0, <=3000
w2 = 912.32, >=0, <=3000
w3 = 912.32, >=0, <=3000
w4 = 912.32, >=0, <=3000
t1 = 0, >=0
t2 = 0, >=0
t3 = 0, >=0
t4 = 0, >=0
Constants
!----------------COEFICIENTES DEL MODELO-----------------!
#Gravedad
g = 9.81 !m/s^2
pi = 3.14159265359
#Motor Coefficients
J = 4.1904e-5 !kg*m^2
kt = 0.0104e-3 !N*m/A
kv = 96.342 !rad/s/volt
Dv = 0.2e-3 !N*m*s/rad
R = 0.2 !Ohms
#Battery parameters
Q = 1.55 !Ah
Rint = 0.02 !Ohms
E0 = 1.24 !volt
K = 2.92e-3 !volt
A = 0.156
B =2.35
#Quadrotor parameters
l = 0.175 !m
m = 1.3 !kg
Ix = 0.081 !kg*m^2
Iy = 0.081 !kg*m^2
Iz = 0.142 !kg*m^2
kb = 3.8305e-6 !N/rad/s
ktau = 2.2518e-8 !(N*m)/rad/s
#Parametrizacion del polinomio
a1 = -1.72e-5
a2 = 1.95e-5
a3 = -6.98e-6
a4 = 4.09e-7
b1 = 0.014
b2 = -0.0157
b3 = 5.656e-3
b4 = -3.908e-4
c1 = -0.8796
c2 = 0.3385
c3 = 0.2890
c4 = 0.1626
Variables
!------------------CONDICONES INICIALES------------------!
x = 0
xp = 0
y = 0
yp = 0
z = 0
zp = 0
pitch = 0, >=-pi/2, <=pi/2 !theta - restricciones
pitchp = 0
roll = 0, >=-pi/2, <=pi/2 !phi - restricciones
rollp = 0
yaw = 0 !psi
yawp = 0%, >=-200/180, <=200/180
#Función objetivo
of = 0 !condición inicial de la función objetivo
Intermediates
#Motor 1
aw1 = a1*w1^2 + b1*w1 + c1
bw1 = a2*w1^2 + b2*w1 + c2
cw1 = a3*w1^2 + b3*w1 + c3
dw1 = a4*w1^2 + b4*w1 + c4
#Motor 2
aw2 = a1*w2^2 + b1*w2 + c1
bw2 = a2*w2^2 + b2*w2 + c2
cw2 = a3*w2^2 + b3*w2 + c3
dw2 = a4*w2^2 + b4*w2 + c4
#Motor 3
aw3 = a1*w3^2 + b1*w3 + c1
bw3 = a2*w3^2 + b2*w3 + c2
cw3 = a3*w3^2 + b3*w3 + c3
dw3 = a4*w3^2 + b4*w3 + c4
#Motor 4
aw4 = a1*w4^2 + b1*w4 + c1
bw4 = a2*w4^2 + b2*w4 + c2
cw4 = a3*w4^2 + b3*w4 + c3
dw4 = a4*w4^2 + b4*w4 + c4
#frj(wj(t),Tj(t))
fr1=aw1*t1^3 + bw1*t1^2 + cw1*t1 + dw1
fr2=aw2*t2^3 + bw2*t2^2 + cw2*t2 + dw2
fr3=aw3*t3^3 + bw3*t3^2 + cw3*t3 + dw3
fr4=aw4*t4^3 + bw4*t4^2 + cw4*t4 + dw4
!---------------------CONTROL INPUTS---------------------!
T = kb * (w1^2 + w2^2 + w3^2 + w4^2)
u1 = kb * (w2^2 - w4^2)
u2 = kb * (w3^2 - w1^2)
u3 = ktau * (w1^2 - w2^2 + w3^2 - w4^2)
wline = w1 - w2 + w3 - w4
!-------------------ENERGIA POR ROTOR--------------------!
Ec1 = ((J*$w1 + ktau*w1^2 + Dv*w1)/fr1)*w1
Ec2 = ((J*$w2 + ktau*w2^2 + Dv*w2)/fr2)*w2
Ec3 = ((J*$w3 + ktau*w3^2 + Dv*w3)/fr3)*w3
Ec4 = ((J*$w4 + ktau*w4^2 + Dv*w4)/fr4)*w4
Ectotal = Ec1 + Ec2 + Ec3 + Ec4
Equations
!---------------MINIMIZAR FUNCIÓN OBJETIVO---------------!
minimize tf * of
!-----------------RELACION DE VARIABLES------------------!
xp = $x
yp = $y
zp = $z
pitchp = $pitch
rollp = $roll
yawp = $yaw
!-----------------CONDICONES DE FRONTERA-----------------!
#Condiciones finales del modelo
tf * x = 4
tf * y = 5
tf * z = 6
tf * xp = 0
tf * yp = 0
tf * zp = 0
tf * roll = 0
tf * pitch = 0
tf * yaw = 0
!-----------------TORQUE DE LOS MOTORES------------------!
t1 = J*$w1 + ktau*w1^2 + Dv*w1
t2 = J*$w2 + ktau*w2^2 + Dv*w2
t3 = J*$w3 + ktau*w3^2 + Dv*w3
t4 = J*$w4 + ktau*w4^2 + Dv*w4
!------------------------SUJETO A------------------------!
#Modelo aerodinámico del UAV
m*$xp = (cos(roll)*sin(pitch)*cos(yaw) + sin(roll)*sin(yaw))*T
m*$yp = (cos(roll)*sin(pitch)*sin(yaw) - sin(roll)*cos(yaw))*T
m*$zp = (cos(roll)*cos(pitch))*T-m*g
Ix*$rollp = ((Iy - Iz)*pitchp*yawp + J*pitchp*wline + l*u1)
Iy*$pitchp = ((Iz - Ix)*rollp*yawp - J*rollp*wline + l*u2)
Iz*$yawp = ((Ix - Iy)*rollp*pitchp + u3)
!--------------------FUNCIÓN OBJETIVO--------------------!
$of = Ectotal
MATLAB:
clear all; close all; clc
server = 'http://127.0.0.1';
app = 'traj_optima';
addpath('C:/Program Files/MATLAB/apm_matlab_v0.7.2/apm')
apm(server,app,'clear all');
apm_load(server,app,'ecuaciones_mod.apm');
csv_load(server,app,'tiempo2.csv');
apm_option(server,app,'apm.max_iter',200);
apm_option(server,app,'nlc.nodes',3);
apm_option(server,app,'apm.rtol',1);
apm_option(server,app,'apm.otol',1);
apm_option(server,app,'nlc.solver',3);
apm_option(server,app,'nlc.imode',6);
apm_option(server,app,'nlc.mv_type',1);
costo=1e-5;%1e-5
%VARIABLES CONTROLADAS
%Velocidades angulares
apm_info(server,app,'MV','w1');
apm_option(server,app,'w1.status',1);
apm_info(server,app,'MV','w2');
apm_option(server,app,'w2.status',1);
apm_info(server,app,'MV','w3');
apm_option(server,app,'w3.status',1);
apm_info(server,app,'MV','w4');
apm_option(server,app,'w4.status',1);
% Torques
apm_info(server,app,'MV','t1');
apm_option(server,app,'t1.status',1);
apm_info(server,app,'MV','t2');
apm_option(server,app,'t2.status',1);
apm_info(server,app,'MV','t3');
apm_option(server,app,'t3.status',1);
apm_info(server,app,'MV','t4');
apm_option(server,app,'t4.status',1);
%Salida
output = apm(server,app,'solve');
disp(output)
y = apm_sol(server,app);
z = y.x;
tiempo2.csv
time,tf
0,0
0.001,0
0.2,0
0.4,0
0.6,0
0.8,0
1,0
1.2,0
1.4,0
1.6,0
1.8,0
2,0
2.2,0
2.4,0
2.6,0
2.8,0
3,0
3.2,0
3.4,0
3.6,0
3.8,0
4,0
4.2,0
4.4,0
4.6,0
4.8,0
5,0
5.2,0
5.4,0
5.6,0
5.8,0
6,0
6.2,0
6.4,0
6.6,0
6.8,0
7,0
7.2,0
7.4,0
7.6,0
7.8,0
8,0
8.2,0
8.4,0
8.6,0
8.8,0
9,0
9.2,0
9.4,0
9.6,0
9.8,0
10,1
Finally the answer obtained is:
enter image description here
I need help with this local infeasibility problem, please.
The infeasible solution is caused by the terminal constraints:
tf * z = 4
tf * z = 5
tf * z = 6
When tf=0, the constraints are evaluated to 0=4, 0=5, 0=6 and the solver reports that these can not be satisfied by the solver. Instead, you can pose the constraints as:
tf * (x-4) = 0
tf * (y-5) = 0
tf * (z-6) = 0
That way, the constraint is valid when tf=0 and when tf=1 at the final time. A potential better way yet is to convert the terminal constraints to objective terms with f=1000 such as:
minimize f*tf*((x-4)^2 + (y-5)^2 + (z-6)^2)
minimize f*tf*(xp^2 + yp^2 + zp^2)
minimize f*tf*(roll^2 + pitch^2 + yaw^2)
That way, the optimizer won't report an infeasible solution if it can't reach the terminal constraints as discussed in the pendulum problem. I made a few other modifications to your model and script to achieve a successful solution. Here is a summary:
Converted terminal constraints to objective function (soft constraints)
Parameters t1-t4 should be variables
Fixed degree of freedom issue by making w1-w4 variables and w1p-w4p variables. w1-w4 are differential states.
Added constraints to w1p-w4p between -10 and 10 to help the solver converge
Added initialization step to simulate the model before optimizing. There are more details on initialization strategies in this paper: Safdarnejad, S.M., Hedengren, J.D., Lewis, N.R., Haseltine, E., Initialization Strategies for Optimization of Dynamic Systems, Computers and Chemical Engineering, 2015, Vol. 78, pp. 39-50, DOI: 10.1016/j.compchemeng.2015.04.016
Model
Parameters
tf
w1p = 0 > -10 < 10
w2p = 0 > -10 < 10
w3p = 0 > -10 < 10
w4p = 0 > -10 < 10
Constants
!----------------COEFICIENTES DEL MODELO-----------------!
#Gravedad
g = 9.81 !m/s^2
pi = 3.14159265359
#Motor Coefficients
J = 4.1904e-5 !kg*m^2
kt = 0.0104e-3 !N*m/A
kv = 96.342 !rad/s/volt
Dv = 0.2e-3 !N*m*s/rad
R = 0.2 !Ohms
#Battery parameters
Q = 1.55 !Ah
Rint = 0.02 !Ohms
E0 = 1.24 !volt
K = 2.92e-3 !volt
A = 0.156
B =2.35
#Quadrotor parameters
l = 0.175 !m
m = 1.3 !kg
Ix = 0.081 !kg*m^2
Iy = 0.081 !kg*m^2
Iz = 0.142 !kg*m^2
kb = 3.8305e-6 !N/rad/s
ktau = 2.2518e-8 !(N*m)/rad/s
#Parametrizacion del polinomio
a1 = -1.72e-5
a2 = 1.95e-5
a3 = -6.98e-6
a4 = 4.09e-7
b1 = 0.014
b2 = -0.0157
b3 = 5.656e-3
b4 = -3.908e-4
c1 = -0.8796
c2 = 0.3385
c3 = 0.2890
c4 = 0.1626
Variables
!------------------CONDICONES INICIALES------------------!
x = 0
xp = 0
y = 0
yp = 0
z = 0
zp = 0
pitch = 0, >=-pi/2, <=pi/2 !theta - restricciones
pitchp = 0
roll = 0, >=-pi/2, <=pi/2 !phi - restricciones
rollp = 0
yaw = 0 !psi
yawp = 0 %, >=-200/180, <=200/180
#Velocidad de rotores rad/s
#Las condiciones iniciales permiten igualar la acción de la gravedad
#Se tomo 4000rad/s como la velocidad maxima de los rotores
w1 = 912.32, >=0, <=3000
w2 = 912.32, >=0, <=3000
w3 = 912.32, >=0, <=3000
w4 = 912.32, >=0, <=3000
t1 = 0, >=0
t2 = 0, >=0
t3 = 0, >=0
t4 = 0, >=0
#Función objetivo
of = 0 !condición inicial de la función objetivo
Intermediates
#Motor 1
aw1 = a1*w1^2 + b1*w1 + c1
bw1 = a2*w1^2 + b2*w1 + c2
cw1 = a3*w1^2 + b3*w1 + c3
dw1 = a4*w1^2 + b4*w1 + c4
#Motor 2
aw2 = a1*w2^2 + b1*w2 + c1
bw2 = a2*w2^2 + b2*w2 + c2
cw2 = a3*w2^2 + b3*w2 + c3
dw2 = a4*w2^2 + b4*w2 + c4
#Motor 3
aw3 = a1*w3^2 + b1*w3 + c1
bw3 = a2*w3^2 + b2*w3 + c2
cw3 = a3*w3^2 + b3*w3 + c3
dw3 = a4*w3^2 + b4*w3 + c4
#Motor 4
aw4 = a1*w4^2 + b1*w4 + c1
bw4 = a2*w4^2 + b2*w4 + c2
cw4 = a3*w4^2 + b3*w4 + c3
dw4 = a4*w4^2 + b4*w4 + c4
#frj(wj(t),Tj(t))
fr1=aw1*t1^3 + bw1*t1^2 + cw1*t1 + dw1
fr2=aw2*t2^3 + bw2*t2^2 + cw2*t2 + dw2
fr3=aw3*t3^3 + bw3*t3^2 + cw3*t3 + dw3
fr4=aw4*t4^3 + bw4*t4^2 + cw4*t4 + dw4
!---------------------CONTROL INPUTS---------------------!
T = kb * (w1^2 + w2^2 + w3^2 + w4^2)
u1 = kb * (w2^2 - w4^2)
u2 = kb * (w3^2 - w1^2)
u3 = ktau * (w1^2 - w2^2 + w3^2 - w4^2)
wline = w1 - w2 + w3 - w4
!-------------------ENERGIA POR ROTOR--------------------!
Ec1 = ((J*$w1 + ktau*w1^2 + Dv*w1)/fr1)*w1
Ec2 = ((J*$w2 + ktau*w2^2 + Dv*w2)/fr2)*w2
Ec3 = ((J*$w3 + ktau*w3^2 + Dv*w3)/fr3)*w3
Ec4 = ((J*$w4 + ktau*w4^2 + Dv*w4)/fr4)*w4
Ectotal = Ec1 + Ec2 + Ec3 + Ec4
! scaling factor for terminal constraint
f = 1000
Equations
!---------------MINIMIZAR FUNCIÓN OBJETIVO---------------!
minimize tf * of
!-----------------RELACION DE VARIABLES------------------!
xp = $x
yp = $y
zp = $z
pitchp = $pitch
rollp = $roll
yawp = $yaw
w1p = $w1
w2p = $w2
w3p = $w3
w4p = $w4
!-----------------CONDICONES DE FRONTERA-----------------!
#Condiciones finales del modelo
#tf * (x-4) = 0
#tf * (y-5) = 0
#tf * (z-6) = 0
#tf * xp = 0
#tf * yp = 0
#tf * zp = 0
#tf * roll = 0
#tf * pitch = 0
#tf * yaw = 0
minimize f*tf*((x-4)^2 + (y-5)^2 + (z-6)^2)
minimize f*tf*(xp^2 + yp^2 + zp^2)
minimize f*tf*(roll^2 + pitch^2 + yaw^2)
!-----------------TORQUE DE LOS MOTORES------------------!
t1 = J*w1p + ktau*w1^2 + Dv*w1
t2 = J*w2p + ktau*w2^2 + Dv*w2
t3 = J*w3p + ktau*w3^2 + Dv*w3
t4 = J*w4p + ktau*w4^2 + Dv*w4
!------------------------SUJETO A------------------------!
#Modelo aerodinámico del UAV
m*$xp = (cos(roll)*sin(pitch)*cos(yaw) + sin(roll)*sin(yaw))*T
m*$yp = (cos(roll)*sin(pitch)*sin(yaw) - sin(roll)*cos(yaw))*T
m*$zp = (cos(roll)*cos(pitch))*T-m*g
Ix*$rollp = ((Iy - Iz)*pitchp*yawp + J*pitchp*wline + l*u1)
Iy*$pitchp = ((Iz - Ix)*rollp*yawp - J*rollp*wline + l*u2)
Iz*$yawp = ((Ix - Iy)*rollp*pitchp + u3)
!--------------------FUNCIÓN OBJETIVO--------------------!
$of = Ectotal
MATLAB Script
clear all; close all; clc
server = 'http://byu.apmonitor.com';
app = 'traj_optima';
addpath('apm')
apm(server,app,'clear all');
apm_load(server,app,'ecuaciones_mod.apm');
csv_load(server,app,'tiempo2.csv');
apm_option(server,app,'apm.max_iter',1000);
apm_option(server,app,'apm.nodes',3);
apm_option(server,app,'apm.rtol',1e-6);
apm_option(server,app,'apm.otol',1e-6);
apm_option(server,app,'apm.solver',3);
apm_option(server,app,'apm.imode',6);
apm_option(server,app,'apm.mv_type',1);
costo=1e-5;%1e-5
%VARIABLES CONTROLADAS
%Velocidades angulares
apm_info(server,app,'MV','w1p');
apm_option(server,app,'w1p.status',1);
apm_info(server,app,'MV','w2p');
apm_option(server,app,'w2p.status',1);
apm_info(server,app,'MV','w3p');
apm_option(server,app,'w3p.status',1);
apm_info(server,app,'MV','w4p');
apm_option(server,app,'w4p.status',1);
%Salida
disp('')
disp('------------- Initialize ----------------')
apm_option(server,app,'apm.coldstart',1);
output = apm(server,app,'solve');
disp(output)
disp('')
disp('-------------- Optimize -----------------')
apm_option(server,app,'apm.time_shift',0);
apm_option(server,app,'apm.coldstart',0);
output = apm(server,app,'solve');
disp(output)
y = apm_sol(server,app);
z = y.x;
This gives a successful solution but the terminal constraints are not met. The solver optimizes the use of w1p-w4p to minimize the objective but there is no solution that makes it to the terminal constraints.
The solution was found.
The final value of the objective function is 50477.4537378181
---------------------------------------------------
Solver : IPOPT (v3.12)
Solution time : 3.06940000000759 sec
Objective : 50477.4537378181
Successful solution
---------------------------------------------------
As a next step, I recommend that you increase the number of time points or allow the final time to change to meet the terminal constraints. You may also want to consider switching to Python Gekko that uses the same underlying engine as APM MATLAB. In this case, the modeling language is fully integrated with Python.

Hmm estimation in R

Im trying to studie likelihood estimation of the transition and emission matrices of an Hmm via a sequence of observation provided from 2 hidden states and 3 possible values at each state
using SeqHmm to generate the sequence
#déclaration des matrices de transition et d 'emission
P.trans <- t(matrix(c(0.2,0.8,
0.7,0.3),nrow=2,ncol=2))
P.emiss <- t(matrix(c(0.2,0.6,0.2,
0.3,0.5,0.2),nrow=3,ncol=2))
# simuler un hmm avec la fonction du package seqHMM
sim <- simulate_hmm(n_sequence=1,transition_probs = P.trans,
emission_probs = P.emiss, initial_probs = c(1/2,1/2),
sequence_length = 100)
# transformer les observations comme un vecteur numeric
sim.obs <- as.numeric(gsub('\\-', ' ', sim$observations))
using Hamilton's 1988 to calculate the log-likelihood function
llh <- function(p,y){
# matrice de transition de la chaine cachée
trans.mat <- matrix(c(1-p[1],p[1],
p[2],1-p[2]), nrow=2, ncol = 2,byrow = TRUE)
# matrice d'émissions
emiss.mat <- matrix(c(p[3],1-p[3]-p[4],p[4],
p[5], p[6],1-p[5]-p[6]), nrow=2,ncol=3,byrow = TRUE)
# la longueur de la chaine
T <- length(y)
Pr <- numeric(T)
#distribution initiale
V <- c(1/2,1/2)
# calcul de la vraisemblance
for(c in 1:T){
w = numeric(nrow(trans.mat))
#i = Y[c-1]
j = y[c]
for (k in 1:nrow(trans.mat)) {
w[k] = 0
for (l in 1:nrow(trans.mat)) {
w[k] = w[k] + V[l]*trans.mat[l,k]*emiss.mat[l,j]
}
}
Pr[c]= sum(w)
V=w/sum(w)
}
# on ajout le - pour minimiser la fonction au lieu de la maximiser
bjf = -sum(log(Pr))
return(bjf)
}
using optim the minimize the - log-likelihood function
prob.init.optim <-c(0.5,0.1,0.3,0.4,0.3,0.7)
# lancer optim
optim(par=prob.init.optim, fn=llh, y=sim.obs)
it returns a negative probabilities values !! does any one can help ?

On the combinational circuit, i want to know propagation delay(path delay)

im new for using vivado tool, and im trying to make multiplier.
and im not using clock for multiplier. Just logic circuit.
and i have problem for to see propagation delay.
can you tell me how i see propagation delay? or if there have no tools for see propagation delay in vivado, how can i measure the delay?
here is my code
`timescale 1ns / 1ns
module full_adder(A, B, C_in, C_out, S );
input A,B,C_in;
output S,C_out;
wire line1;
wire line2;
wire line3;
wire line4;
wire line5;
assign line1 = A^B,
line2 = line1 ^ C_in,
line3 = line1 & C_in,
line4 = A & B,
line5 = line3 | line4;
assign S = line2;
assign C_out = line5;
endmodule
`timescale 1ns / 1ns
module three_input_FA(a,b,v,h,
s_in,
s_out, c_in, c_out );
input a, b, v, h, s_in, c_in;
output s_out, c_out;
wire vh;
wire vhab;
assign vh = v ^ h;
assign vhab = vh & a & b;
full_adder inst1(s_in, vhab, c_in, c_out, s_out);
endmodule
`timescale 1ns / 1ps
module useful_2by2(
a,b,v,h,s_in,s_out,c_in,c_out
);
input [1:0] a;
input [1:0] b;
input [1:0] v;
input [1:0] h;
output [2:0] s_in;
output [2:0] s_out;
output [1:0] c_in;
output [1:0] c_out;
wire [2:0]s0_in;
wire [3:0]s0_out;
wire [1:0]c0_in;
wire [3:0]c0_out;
three_input_FA inst1(a[0],b[0], v[0],h[0], s0_in[0], s0_out[0], c0_in[0], c0_out[0]); //inst1의 c_out1이 inst2의 c_in 부분으로 연결
three_input_FA inst2(a[1],b[0], v[1],h[0], s0_in[1], s0_out[1], c0_out[0], c0_out[1]);
three_input_FA inst3(a[0],b[1], v[0],h[1], s0_out[1],s0_out[2], c0_in[1], c0_out[2]);
three_input_FA inst4(a[1],b[1], v[1],h[1], s0_in[2], s0_out[3], c0_out[2], c0_out[3]);
assign c_out[0] = c0_out[1], c_out[1] = c0_out[3];
assign s_out[0] = s0_out[0], s_out[1] = s0_out[2], s_out[2] = s0_out[3];
assign c0_in[0] = c_in[0] , c0_in[1] = c_in[1];
assign s0_in[0] = s_in[0], s0_in[1] = s_in[1], s0_in[2] = s_in[2];
endmodule
`timescale 1ns / 1ps
module useful_4by4(
a,b,v,h,s_in,s_out,c_in,c_out
);
input [3:0]a;
input [3:0]b;
input [3:0] v;
input [3:0] h;
output [6:0]s_in;
output [6:0]s_out;
output [3:0]c_in;
output [3:0]c_out;
wire [11:0] s0_in;
wire [11:0] s0_out;
wire [7:0] c0_in;
wire [7:0] c0_out;
useful_2by2 inst1(a[1:0],b[1:0], v[1:0], h[1:0], s0_in[2:0], s0_out[2:0], c0_in[1:0], c0_out[1:0]);
useful_2by2 inst2(a[3:2],b[1:0], v[3:2], h[1:0], s0_in[5:3], s0_out[5:3], c0_in[3:2], c0_out[3:2]);
useful_2by2 inst3(a[1:0],b[3:2], v[1:0], h[3:2], s0_in[8:6], s0_out[8:6], c0_in[5:4], c0_out[5:4]);
useful_2by2 inst4(a[3:2],b[3:2], v[3:2], h[3:2], s0_in[11:9], s0_out[11:9], c0_in[7:6], c0_out[7:6]);
assign c0_in[2] = c0_out[0], c0_in[3] = c0_out[1],
c0_in[6] = c0_out[4], c0_in[7] = c0_out[5];
assign s0_in[6] = s0_out[2], s0_in[7] = s0_out[4],
s0_in[9] = s0_out[5],
s0_in[2] = s0_out[3], s0_in[8] = s0_out[9];
assign c_out[0] = c0_out[2], c_out[1] = c0_out[3], c_out[2] = c0_out[6], c_out[3] = c0_out[7];
assign s0_in[0] = s_in[0], s0_in[1] = s_in[1], s0_in[3] = s_in[2], s0_in[4] = s_in[3],
s0_in[5] = s_in[4], s0_in[10] = s_in[5], s0_in[11] = s_in[6];
assign c0_in[0] = c_in[0], c0_in[1] = c_in[1], c0_in[4] = c_in[2], c0_in[5] = c_in[3];
assign s_out[0] = s0_out[0], s_out[1] = s0_out[1], s_out[2] = s0_out[6], s_out[3] = s0_out[7],
s_out[4] = s0_out[8], s_out[5] = s0_out[10], s_out[6] = s0_out[11];
//s_in to c_out link
/*assign s_in[4] = c_out[0], s_in[5] = c_out[1], s_in[6] = c_out[2];*/
endmodule
`timescale 1ns / 1ps
module useful_8by8(
a,b,v,h,s_in,s_out,c_in,c_out
);
input [7:0]a;
input [7:0]b;
input [7:0] v;
input [7:0] h;
output [14:0]s_in;
output [14:0]s_out;
output [7:0]c_in;
output [7:0]c_out;
wire [27:0]s0_in;
wire [27:0]s0_out;
wire [15:0]c0_in;
wire [15:0]c0_out;
useful_4by4 inst1(a[3:0], b[3:0], v[3:0], h[3:0], s0_in[6:0], s0_out[6:0], c0_in[3:0], c0_out[3:0]);
useful_4by4 inst2(a[7:4], b[3:0], v[7:4], h[3:0], s0_in[13:7], s0_out[13:7], c0_in[7:4], c0_out[7:4]);
useful_4by4 inst3(a[3:0], b[7:4], v[3:0], h[7:4], s0_in[20:14], s0_out[20:14], c0_in[11:8], c0_out[11:8]);
useful_4by4 inst4(a[7:4], b[7:4], v[7:4], h[7:4], s0_in[27:21], s0_out[27:21], c0_in[15:12], c0_out[15:12]);
assign c0_in[4] = c0_out[0], c0_in[5] = c0_out[1], c0_in[6] = c0_out[2], c0_in[7] = c0_out[3],
c0_in[12] = c0_out[8], c0_in[13] = c0_out[9], c0_in[14] = c0_out[10], c0_in[15] = c0_out[11];
assign s0_in[14] = s0_out[4], s0_in[15] = s0_out[5], s0_in[16] = s0_out[6], s0_in[17] = s0_out[10],
s0_in[21] = s0_out[11], s0_in[22] = s0_out[12], s0_in[23] = s0_out[13],
s0_in[4] = s0_out[7], s0_in[5] = s0_out[8], s0_in[6] = s0_out[9], s0_in[18] = s0_out[21],
s0_in[19] = s0_out[22], s0_in[20] = s0_out[23];
//output
assign c_out[0] = c0_out[4], c_out[1] = c0_out[5], c_out[2] = c0_out[6], c_out[3] = c0_out[7],
c_out[4] = c0_out[12], c_out[5] = c0_out[13], c_out[6] = c0_out[14], c_out[7] = c0_out[15];
assign s0_in[0] = s_in[0], s0_in[1] = s_in[1], s0_in[2] = s_in[2], s0_in[3] = s_in[3],
s0_in[7] = s_in[4], s0_in[8] = s_in[5], s0_in[9] = s_in[6], s0_in[10] = s_in[7],
s0_in[11] = s_in[8], s0_in[12] = s_in[9], s0_in[13] = s_in[10], s0_in[24] = s_in[11],
s0_in[25] = s_in[12], s0_in[26] = s_in[13], s0_in[27] = s_in[14];
assign c0_in[0] = c_in[0], c0_in[1] = c_in[1], c0_in[2] = c_in[2], c0_in[3] = c_in[3],
c0_in[8] = c_in[4], c0_in[9] = c_in[5], c0_in[10] = c_in[6],c0_in[11] = c_in[7];
assign s_out[0] = s0_out[0], s_out[1] = s0_out[1], s_out[2] = s0_out[2], s_out[3] = s0_out[3],
s_out[4] = s0_out[14], s_out[5] = s0_out[15], s_out[6] = s0_out[16], s_out[7] = s0_out[17],
s_out[8] = s0_out[18], s_out[9] = s0_out[19], s_out[10] = s0_out[20], s_out[11] = s0_out[24],
s_out[12] = s0_out[25], s_out[13] = s0_out[26], s_out[14] = s0_out[27];
// c_out to s_in link
/*assign s_in[8] = c_out[0], s_in[9] = c_out[1], s_in[10] = c_out[2], s_in[11] = c_out[3],
s_in[12] = c_out[4], s_in[13] = c_out[5], s_in[14] = c_out[6];*/
endmodule
`timescale 1ns / 1ps
module module_16by16(
a,b,v,h,s_in,s_out,c_in,c_out
);
input [15:0]a;
input [15:0]b;
input [15:0] v;
input [15:0] h;
output [30:0]s_in;
output [30:0]s_out;
output [15:0]c_in;
output [15:0]c_out;
wire [59:0]s0_in;
wire [59:0]s0_out;
wire [31:0]c0_in;
wire [31:0]c0_out;
useful_8by8 inst1(a[7:0], b[7:0], v[7:0], h[7:0], s0_in[14:0], s0_out[14:0], c0_in[7:0], c0_out[7:0]);
useful_8by8 inst2(a[15:8],b[7:0], v[15:8], h[7:0], s0_in[29:15], s0_out[29:15], c0_in[15:8], c0_out[15:8]);
useful_8by8 inst3(a[7:0], b[15:8], v[7:0], h[15:8], s0_in[44:30], s0_out[44:30], c0_in[23:16], c0_out[23:16]);
useful_8by8 inst4(a[15:8],b[15:8], v[15:8], h[15:8], s0_in[59:45], s0_out[59:45], c0_in[31:24], c0_out[31:24]);
assign c0_in[8] = c0_out[0], c0_in[9] = c0_out[1], c0_in[10] = c0_out[2], c0_in[11] = c0_out[3],
c0_in[12] = c0_out[4], c0_in[13] = c0_out[5], c0_in[14] = c0_out[6], c0_in[15] = c0_out[7],
c0_in[24] = c0_out[16], c0_in[25] = c0_out[17], c0_in[26] = c0_out[18], c0_in[27] = c0_out[19],
c0_in[28] = c0_out[20], c0_in[29] = c0_out[21], c0_in[30] = c0_out[22], c0_in[31] = c0_out[23];
assign s0_in[30] = s0_out[8], s0_in[31] = s0_out[9], s0_in[32] = s0_out[10], s0_in[33] = s0_out[11],
s0_in[34] = s0_out[12], s0_in[35] = s0_out[13], s0_in[36] = s0_out[14], s0_in[37] = s0_out[22],
s0_in[45] = s0_out[23], s0_in[46] = s0_out[24], s0_in[47] = s0_out[25], s0_in[48] = s0_out[26],
s0_in[49] = s0_out[27], s0_in[50] = s0_out[28], s0_in[51] = s0_out[29],
s0_in[8] = s0_out[15], s0_in[9] = s0_out[16], s0_in[10] = s0_out[17], s0_in[11] = s0_out[18],
s0_in[12] = s0_out[19], s0_in[13] = s0_out[20], s0_in[14] = s0_out[21],
s0_in[38] = s0_out[45], s0_in[39] = s0_out[46], s0_in[40] = s0_out[47], s0_in[41] = s0_out[48],
s0_in[42] = s0_out[49], s0_in[43] = s0_out[50], s0_in[44] = s0_out[51];
//output
assign c0_in[0] = c_in[0], c0_in[1] = c_in[1], c0_in[2] = c_in[2], c0_in[3] = c_in[3],
c0_in[4] = c_in[4], c0_in[5] = c_in[5], c0_in[6] = c_in[6], c0_in[7] = c_in[7],
c0_in[16] = c_in[8], c0_in[17] = c_in[9], c0_in[18] = c_in[10], c0_in[19] = c_in[11],
c0_in[20] = c_in[12], c0_in[21] = c_in[13], c0_in[22] = c_in[14], c0_in[23] = c_in[15];
assign c_out[0] = c0_out[8], c_out[1] = c0_out[9], c_out[2] = c0_out[10], c_out[3] = c0_out[11],
c_out[4] = c0_out[12], c_out[5] = c0_out[13], c_out[6] = c0_out[14], c_out[7] = c0_out[15],
c_out[8] = c0_out[24], c_out[9] = c0_out[25], c_out[10] = c0_out[26], c_out[11] = c0_out[27],
c_out[12] = c0_out[28], c_out[13] = c0_out[29], c_out[14] = c0_out[30], c_out[15] = c0_out[31];
assign s0_in[0] = s_in[0], s0_in[1] = s_in[1], s0_in[2] = s_in[2], s0_in[3] = s_in[3], s0_in[4] = s_in[4], s0_in[5] = s_in[5],
s0_in[6] = s_in[6], s0_in[7] = s_in[7], s0_in[15] = s_in[8], s0_in[16] = s_in[9], s0_in[17] = s_in[10],s0_in[18] = s_in[11],
s0_in[19] = s_in[12],s0_in[20] = s_in[13],s0_in[21] = s_in[14],s0_in[22] = s_in[15],s0_in[23] = s_in[16],s0_in[24] = s_in[17],
s0_in[25] = s_in[18],s0_in[26] = s_in[19],s0_in[27] = s_in[20],s0_in[28] = s_in[21],s0_in[29] = s_in[22],s0_in[52] = s_in[23],
s0_in[53] = s_in[24],s0_in[54] = s_in[25],s0_in[55] = s_in[26],s0_in[56] = s_in[27],s0_in[57] = s_in[28],s0_in[58] = s_in[29],
s0_in[59] = s_in[30];
assign s_out[0] = s0_out[0], s_out[1] = s0_out[1], s_out[2] = s0_out[2], s_out[3] = s0_out[3], s_out[4] = s0_out[4], s_out[5] = s0_out[5],
s_out[6] = s0_out[6], s_out[7] = s0_out[7], s_out[8] = s0_out[30], s_out[9] = s0_out[31], s_out[10] = s0_out[32], s_out[11] = s0_out[33],
s_out[12] = s0_out[34], s_out[13] = s0_out[35], s_out[14] = s0_out[36], s_out[15] = s0_out[37], s_out[16] = s0_out[38], s_out[17] = s0_out[39],
s_out[18] = s0_out[40], s_out[19] = s0_out[41], s_out[20] = s0_out[42], s_out[21] = s0_out[43], s_out[22] = s0_out[44], s_out[23] = s0_out[52],
s_out[24] = s0_out[53], s_out[25] = s0_out[54], s_out[26] = s0_out[55], s_out[27] = s0_out[56], s_out[28] = s0_out[57], s_out[29] = s0_out[58],
s_out[30] = s0_out[59];
//c_out link to s_in
assign s_in[16] = c_out[0], s_in[17] = c_out[1], s_in[18] = c_out[2], s_in[19] = c_out[3],
s_in[20] = c_out[4], s_in[21] = c_out[5], s_in[22] = c_out[6], s_in[23] = c_out[7],
s_in[24] = c_out[8], s_in[25] = c_out[9], s_in[26] = c_out[10], s_in[27] = c_out[11],
s_in[28] = c_out[12], s_in[29] = c_out[13], s_in[30] = c_out[14];
endmodule
`timescale 1ns / 1ps
module top_16by16(
a,b,v,h,p
);
input [15:0] a;
input [15:0] b;
input [15:0] v;
input [15:0] h;
output [31:0]p;
wire [30:0]s_in;
wire [30:0]s_out;
wire [15:0]c_in;
wire [15:0]c_out;
module_16by16 inst1(a,b,v,h,s_in,s_out,c_in,c_out);
assign s_in[0] = 0, s_in[1] = 0, s_in[2] = 0, s_in[3] = 0, s_in[4] = 0, s_in[5] = 0, s_in[6] = 0, s_in[7] = 0,
s_in[8] = 0, s_in[9] = 0, s_in[10] = 0, s_in[11] = 0, s_in[12] = 0, s_in[13] = 0, s_in[14] = 0, s_in[15] = 0;
assign c_in[0] = 0, c_in[1] = 0, c_in[2] = 0, c_in[3] = 0, c_in[4] = 0, c_in[5] = 0, c_in[6] = 0, c_in[7] = 0,
c_in[8] = 0, c_in[9] = 0, c_in[10] = 0, c_in[11] = 0, c_in[12] = 0, c_in[13] = 0, c_in[14] = 0, c_in[15] = 0;
assign p[0] = s_out[0], p[1] = s_out[1], p[2] = s_out[2], p[3] = s_out[3],
p[4] = s_out[4], p[5] = s_out[5], p[6] = s_out[6], p[7] = s_out[7],
p[8] = s_out[8], p[9] = s_out[9], p[10] = s_out[10], p[11] = s_out[11],
p[12] = s_out[12], p[13] = s_out[13], p[14] = s_out[14], p[15] = s_out[15],
p[16] = s_out[16], p[17] = s_out[17], p[18] = s_out[18], p[19] = s_out[19],
p[20] = s_out[20], p[21] = s_out[21], p[22] = s_out[22], p[23] = s_out[23],
p[24] = s_out[24], p[25] = s_out[25], p[26] = s_out[26], p[27] = s_out[27],
p[28] = s_out[28], p[29] = s_out[29], p[30] = s_out[30], p[31] = c_out[15];
endmodule
`timescale 1ns / 1ps
module test_16by16();
reg [15:0] a;
reg [15:0] b;
reg [15:0] v;
reg [15:0] h;
wire [31:0] p;
integer i0,i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,
j0,j1,j2,j3,j4,j5,j6,j7,j8,j9,j10,j11,j12,j13,j14,j15;
top_16by16 test(
.a(a),.b(b),
.v(v),.h(h),
.p(p)
);
initial begin
a[15:0] = 1'b0;
b[15:0] = 1'b0;
v[0] = 0; h[0] = 1; v[4] = 0; h[4] = 1;
v[1] = 0; h[1] = 1; v[5] = 0; h[5] = 1;
v[2] = 0; h[2] = 1; v[6] = 0; h[6] = 1;
v[3] = 0; h[3] = 1; v[7] = 0; h[7] = 1;
v[8] = 0; h[8] = 1; v[12] = 0; h[12] = 1;
v[9] = 0; h[9] = 1; v[13] = 0; h[13] = 1;
v[10] = 0; h[10] = 1; v[14] = 0; h[14] = 1;
v[11] = 0; h[11] = 1; v[15] = 0; h[15] = 1;
for(i0=0;i0<2;i0=i0+1) //a15
begin
for(i1=0;i1<2;i1=i1+1) //a14
begin
for(i2=0;i2<2;i2=i2+1) //a13
begin
for(i3=0;i3<2;i3=i3+1) //a12
begin
for(i4=0;i4<2;i4=i4+1)//a11
begin
for(i5=0;i5<2;i5=i5+1)//a10
begin
for(i6=0;i6<2;i6=i6+1)//a9
begin
for(i7=0;i7<2;i7=i7+1)//a8
begin
for(i8=0;i8<2;i8=i8+1) //a7
begin
for(i9=0;i9<2;i9=i9+1) //a6
begin
for(i10=0;i10<2;i10=i10+1) //a5
begin
for(i11=0;i11<2;i11=i11+1) //a4
begin
for(i12=0;i12<2;i12=i12+1) //a3
begin
for(i13=0;i13<2;i13=i13+1) //a2
begin
for(i14=0;i14<2;i14=i14+1) //a1
begin
for(i15=0;i15<2;i15=i15+1) //a0
begin
for(j0=0;j0<2;j0=j0+1) //b15
begin
for(j1=0;j1<2;j1=j1+1)//b14
begin
for(j2=0;j2<2;j2=j2+1) //b13
begin
for(j3=0;j3<2;j3=j3+1)//b12
begin
for(j4=0;j4<2;j4=j4+1)//b11
begin
for(j5=0;j5<2;j5=j5+1)//b10
begin
for(j6=0;j6<2;j6=j6+1)//b9
begin
for(j7=0;j7<2;j7=j7+1)//b8
begin
for(j8=0;j8<2;j8=j8+1)//b7
begin
for(j9=0;j9<2;j9=j9+1)//b6
begin
for(j10=0;j10<2;j10=j10+1)//b5
begin
for(j11=0;j11<2;j11=j11+1)//b4
begin
for(j12=0;j12<2;j12=j12+1)//b3
begin
for(j13=0;j13<2;j13=j13+1)//b2
begin
for(j14=0;j14<2;j14=j14+1)//b1
begin
for(j15=0;j15<2;j15=j15+1)//b0
begin
b[0] = b[0] + 1'b1; #10;
end
b[1] = b[1] + 1'b1;
end
b[2] = b[2] + 1'b1;
end
b[3] = b[3] + 1'b1;
end
b[4] = b[4] + 1'b1;
end
b[5] = b[5] + 1'b1;
end
b[6] = b[6] + 1'b1;
end
b[7] = b[7] + 1'b1;
end
b[8] = b[8] + 1'b1;
end
b[9] = b[9] + 1'b1;
end
b[10] = b[10] + 1'b1;
end
b[11] = b[11] + 1'b1;
end
b[12] = b[12] + 1'b1;
end
b[13] = b[13] + 1'b1;
end
b[14] = b[14] + 1'b1;
end
b[15] = b[15] + 1'b1;
end
a[0] = a[0] + 1'b1;
end
a[1] = a[1] + 1'b1;
end
a[2] = a[2] + 1'b1;
end
a[3] = a[3]+ 1'b1;
end
a[4] = a[4] + 1'b1;
end
a[5] = a[5] + 1'b1;
end
a[6] = a[6] + 1'b1;
end
a[7] = a[7] + 1'b1;
end
a[8] = a[8] + 1'b1;
end
a[9] = a[9] + 1'b1;
end
a[10] = a[10] + 1'b1;
end
a[11] = a[11] + 1'b1;
end
a[12] = a[12] + 1'b1;
end
a[13] = a[13] + 1'b1;
end
a[14] = a[14] + 1'b1;
end
a[15]= a[15] + 1'b1;
end
end
endmodule
First, do you think your code is readable and maintainable?
Second, multiplication can be done with a multiplication operator instead of nested loops. More over the multiplication algorithm is 2 nested loops of 1-bit additions. I don't know how your algorithm can use so many nested loops and do a multiplication...
Propagation delays are calculated by static timing analysis (STA). STA need timing information about your circuit. The easiest way is to specify a clock and to register all inputs and outputs. This will calculate the FF-to-FF delay paths for you.

Find the distance from camera to vanishing point in matlab

I have this program that finds the vanishing point for a given set of images. Is there a way to find the distance from the camera and the vanishing point?
Also once the vanishing point is found out, I manually need to find the X and Y coordinates using the tool provided in matlab. How can i code a snippet that writes all the X and Y coordinates into a text or excel file?
Also is there a better and simpler way to find the vanishing point in matlab?
Matlab Calling Function to find Vanishing Point:
clear all; close all;
dname = 'Height';
files = dir(dname);
files(1) = [];
files(1) = [];
for i=1:size(files, 1)
original = imread(fullfile(dname, files(i).name));
original = imresize(original,0.35);
im = im2double(rgb2gray(original));
[row, col] = findVanishingPoint(im);
imshow(original);hold;plot(col,row,'rx');
saveas(gcf,strcat('Height_Result',num2str(i)),'jpg');
close
end
The findVanishingPoint function:
function [row, col] = findVanishingPoint(im)
DEBUG = 0;
IM = fft2(im);
ROWS = size(IM,1); COLS = size(IM,2);
PERIOD = 2^floor(log2(COLS)-5)+2;
SIZE = floor(10*PERIOD/pi);
SIGMA = SIZE/9;
NORIENT = 72;
E = 8;
[C, S] = createGaborBank(SIZE, PERIOD, SIGMA, NORIENT, ROWS, COLS, E);
D = ones(ROWS, COLS);
AMAX = ifftshift(real(ifft2(C{1}.*IM)).^2+real(ifft2(S{1}.*IM))).^2;
for n=2:NORIENT
A = ifftshift(real(ifft2(C{n}.*IM)).^2+real(ifft2(S{n}.*IM))).^2;
D(find(A > AMAX)) = n;
AMAX = max(A, AMAX);
if (DEBUG==1)
colormap('hot');subplot(131);imagesc(real(A));subplot(132);imagesc(real(AMAX));colorbar;
subplot(133);imagesc(D);
pause
end
end
if (DEBUG==2)
figure('DoubleBuffer','on');
end
T = mean(AMAX(:))-3*std(AMAX(:));
VOTE = zeros(ROWS, COLS);
for row=round(1+SIZE/2):round(ROWS-SIZE/2)
for col=round(1+SIZE/2):round(COLS-SIZE/2)
if (AMAX(row,col) > T)
indices = lineBresenham(ROWS, COLS, col, row, D(row, col)*pi/NORIENT-pi/2);
VOTE(indices) = VOTE(indices)+AMAX(row,col);
end
end
if (DEBUG==2)
colormap('hot');imagesc(VOTE);pause;
end
end
if (DEBUG==2)
close
end
M=1;
[b index] = sort(-VOTE(:));
col = floor((index(1:M)-1) / ROWS)+1;
row = mod(index(1:M)-1, ROWS)+1;
col = round(mean(col));
row = round(mean(row));
The creatGaborBank function:
function [C, S] = createGaborBank(SIZE, PERIOD, SIGMA, NORIENT, ROWS, COLS, E)
if (length(NORIENT)==1)
orientations=[1:NORIENT];
else
orientations = NORIENT;
NORIENT = max(orientations);
end
for n=orientations
[C{n}, S{n}] = gabormask(SIZE, SIGMA, PERIOD, n*pi/NORIENT);
C{n} = fft2(padWithZeros(C{n}, ROWS, COLS));
S{n} = fft2(padWithZeros(S{n}, ROWS, COLS));
end
The gabormask function:
function [cmask, smask] = gabormask(Size, sigma, period, orient, E)
if nargin < 5; E = 8; end;
if nargin < 4; orient = 0; end;
if nargin < 3; period = []; end;
if nargin < 2; sigma = []; end;
if nargin < 1; Size = []; end;
if isempty(period) & isempty(sigma); sigma = 5; end;
if isempty(period); period = sigma*2*sqrt(2); end;
if isempty(sigma); sigma = period/(2*sqrt(2)); end;
if isempty(Size); Size = 2*round(2.575*sigma) + 1; end;
if length(Size) == 1
sx = Size-1; sy = sx;
elseif all(size(Size) == [1 2])
sy = Size(1)-1; sx = Size(2)-1;
else
error('Size must be scalar or 1-by-2 vector');
end;
hy = sy/2; hx = sx/2;
[x, y] = meshgrid(-hx:sx-hx, -hy:sy-hy);
omega = 2*pi/period;
cs = omega * cos(orient);
sn = omega * sin(orient);
k = -1/(E*sigma*sigma);
g = exp(k * (E*x.*x + y.*y));
xp = x * cs + y * sn;
cx = cos(xp);
cmask = g .* cx;
sx = sin(xp);
smask = g .* sx;
cmask = cmask - mean(cmask(:));
cmask = cmask/sum(abs(cmask(:)));
smask = smask - mean(smask(:));
smask = smask/sum(abs(smask(:)));
The padWithZeros function:
function out = padWithZeros(in, ROWS, COLS)
out = padarray(in,[floor((ROWS-size(in,1))/2) floor((COLS-size(in,2))/2)],0,'both');
if size(out,1) == ROWS-1
out = padarray(out,[1 0],0,'pre');
end
if size(out,2) == COLS-1
out = padarray(out,[0 1],0,'pre');
end
The findHorizonEdge function:
function row = findHorizon(im)
DEBUG = 2;
ROWS = size(im,1); COLS = size(im,2);
e = edge(im,'sobel', [], 'horizontal');
dd = sum(e, 2);
N=3;
row = 1;
M = 0;
for i=1+N:length(dd)-N
m = sum(dd(i-N:i+N));
if (m > M)
M = m;
row = i;
end
end
imshow(e);pause
The findHorizon function:
function row = findHorizon(im)
DEBUG = 2;
IM = fft2(im);
ROWS = size(IM,1); COLS = size(IM,2);
PERIOD = 2^floor(log2(COLS)-5)+2;
SIZE = floor(10*PERIOD/pi);
SIGMA = SIZE/9;
NORIENT = 72;
E = 16;
orientations = [NORIENT/2-10:NORIENT/2+10];
[C, S] = createGaborBank(SIZE, PERIOD, SIGMA, orientations, ROWS, COLS, E);
ASUM = zeros(ROWS, COLS);
for n=orientations
A = ifftshift(real(ifft2(C{n}.*IM)).^2+real(ifft2(S{n}.*IM))).^2;
ASUM = ASUM + A;
if (DEBUG==1)
colormap('hot');subplot(131);imagesc(real(A));subplot(132);imagesc(real(AMAX));colorbar;
pause
end
end
ASUM(1:round(1+SIZE/2), :)=0; ASUM(end-round(SIZE/2):end, :)=0;
ASUM(:,end-round(SIZE/2):end)=0; ASUM(:, 1:1+round(SIZE/2))=0;
dd = sum(ASUM, 2);
[temp, row] = sort(-dd);
row = round(mean(row(1:10)));
if (DEBUG == 2)
imagesc(ASUM);hold on;line([1:COLS],repmat(row,COLS));
pause
end
The lineImage function:
function v = lineimage(x0, y0, angle, s)
if (abs(tan(angle)) > 1e015)
a(1,:) = repmat(x0,s(1),1)';
a(2,:) = [1:s(1)];
elseif (abs(tan(angle)) < 1e-015)
a(2,:) = repmat(y0,s(2),1)';
a(1,:) = [1:s(2)];
else
k = tan(angle);
hiX = round((1-(s(1)-y0+1)+k*x0)/k);
loX = round((s(1)-(s(1)-y0+1)+k*x0)/k);
temp = max(loX, hiX);
loX = max(min(loX, hiX), 1);
hiX = min(s(2),temp);
a(1,:) = [loX:hiX];
a(2,:) = max(1, floor(s(1)-(k*a(1,:)+(s(1)-y0+1)-k*x0)));
end
v = (a(1,:)-1).*s(1)+a(2,:);
The lineVector function:
function [abscissa, ordinate] = linevector(x0, y0, angle, s)
if (rad2deg(angle) == 90)
abscissa = repmat(x0,s(1),1);
ordinate = [1:s(1)];
else
k = tan(angle);
hiX = round((1-(s(1)-y0+1)+k*x0)/k);
loX = round((s(1)-(s(1)-y0+1)+k*x0)/k);
temp = max(loX, hiX);
loX = max(min(loX, hiX), 1);
hiX = min(s(2),temp);
abscissa = [loX:hiX];
ordinate = k*abscissa+((s(1)-y0+1)-k*x0);
end
The lineBresenham function:
function [i] = lineBresenham(H,W,Sx,Sy,angle)
k = tan(angle);
if (angle == pi || angle == 0)
Ex = W;
Ey = Sy;
Sx = 1;
elseif (angle == pi/2)
Ey = 1;
i = (Sx-1)*H+[Ey:Sy];
return;
elseif k>0 & k < (Sy-1)/(W-Sx)
Ex = W;
Ey = round(Sy-tan(angle)*(Ex-Sx));
elseif k < 0 & abs(k) < (Sy-1)/(Sx-1)
Ex = 1;
Ey = round(Sy-tan(angle)*(Ex-Sx));
else
Ey = 1;
Ex = round((Sy-1)/tan(angle)+Sx);
end
Dx = Ex - Sx;
Dy = Ey - Sy;
iCoords=1;
if(abs(Dy) <= abs(Dx))
if(Ex >= Sx)
D = 2*Dy + Dx;
IncH = 2*Dy;
IncD = 2*(Dy + Dx);
X = Sx;
Y = Sy;
i(iCoords) = (Sx-1)*H+Sy;
iCoords = iCoords + 1;
while(X < Ex)
if(D >= 0)
D = D + IncH;
X = X + 1;
else
D = D + IncD;
X = X + 1;
Y = Y - 1;
end
i(iCoords) = (X-1)*H+Y;
iCoords = iCoords + 1;
end
else
D = -2*Dy + Dx;
IncH = -2*Dy;
IncD = 2*(-Dy + Dx);
X = Sx;
Y = Sy;
i(iCoords) = (Sx-1)*H+Sy;
iCoords = iCoords + 1;
while(X > Ex)
if(D <= 0)
D = D + IncH;
X = X - 1;
else
D = D + IncD;
X = X - 1;
Y = Y - 1;
end
i(iCoords) = (X-1)*H+Y;
iCoords = iCoords + 1;
end
end
else
Tmp = Ex;
Ex = Ey;
Ey = Tmp;
Tmp = Sx;
Sx = Sy;
Sy = Tmp;
Dx = Ex - Sx;
Dy = Ey - Sy;
if(Ex >= Sx)
D = 2*Dy + Dx;
IncH = 2*Dy;
IncD = 2*(Dy + Dx);
X = Sx;
Y = Sy;
i(iCoords) = (Sy-1)*H+Sx;
iCoords = iCoords + 1;
while(X < Ex)
if(D >= 0)
D = D + IncH;
X = X + 1;
else
D = D + IncD;
X = X + 1;
Y = Y - 1;
end
i(iCoords) = (Y-1)*H+X;
iCoords = iCoords + 1;
end
else
D = -2*Dy + Dx;
IncH = -2*Dy;
IncD = 2*(-Dy + Dx);
X = Sx;
Y = Sy;
i(iCoords) = (Sy-1)*H+Sx;
iCoords = iCoords + 1;
while(X > Ex)
if(D <= 0)
D = D + IncH;
X = X - 1;
else
D = D + IncD;
X = X - 1;
Y = Y - 1;
end
i(iCoords) = (Y-1)*H+X;
iCoords = iCoords + 1;
end
end
end
The vanishing point is at infinity hence the distance to the camera is of no use.
Use xlswrite or dlmwrite to write into excel or text file respectively.

Resources