On the combinational circuit, i want to know propagation delay(path delay) - fpga
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.
Related
Matlab cell to matrix vectorization
I have a cell with 3 synchronized signals in each column. I want to extract specific segments into my seg cell. How do I get rid of the loop here? data = cell(1, 3); data{1,1} = [ones(30, 1)]; data{1,2} = [1:30]'; data{1,3} = 2*[1:30]'; ind1 = [1; 11; 17]; ind2 = [10; 16; 30]; for i = 1:3 seg{i, 1} = [data{:,1}(ind1(i):ind2(i)) data{:,2}(ind1(i):ind2(i)) data{:,3}(ind1(i):ind2(i))]; end
You mean like this? data = cell(1, 3); data{1,1} = ones(30, 1); data{1,2} = (1:30)'; data{1,3} = 2*(1:30)'; D = cell2mat(data); Cel = mat2cell(D,[10 6 14],3);
Scaling the image using Bilinear Interpolation - matlab
I have written a code that reads an image and does scaling of 2 units in x-axis direction. Scaling matrix is filled by values that are read from a text file. Scaling Matrix looks like 2 0 0 0 1 0 0 0 1 Original Image Transformed Image (Scaling of 2 units in X-direction) Code file = importdata('transform_c.txt'); fileData = file.data; image = imread('mecca06.pgm'); [row, col] = size(image); scalingMatrix = zeros(3,3); scalingMatrix(1,1) = fileData(2); scalingMatrix(1,2) = fileData(3); scalingMatrix(1,3) = fileData(4); scalingMatrix(2,1) = fileData(5); scalingMatrix(2,2) = fileData(6); scalingMatrix(2,3) = fileData(7); scalingMatrix(3,1) = fileData(8); scalingMatrix(3,2) = fileData(9); scalingMatrix(3,3) = fileData(10); m1Inverse = inv(scalingMatrix); outputImage = applyTransformation(image, row, col, m1Inverse); figure imshow(outputImage); function outImage = applyTransformation(image, row, col, m1Inverse) points = zeros(3,1); for i=1:row for j=1:col points(1,1) = i; points(2,1) = j; points(3,1) = 1; m2 = m1Inverse * points; x = m2(1,1); y = m2(2,1); xlb = floor(x); ylb = floor(y); if(xlb <= 0) xlb = 1; end if(xlb > row) xlb = row; end if(ylb <= 0) ylb = 1; end if(ylb > col) ylb = col; end xub = xlb+1; yub = ylb+1; if(xub <= 0) xub = 1; end if(xub > row) xub = row; end if(yub <= 0) yub = 1; end if(yub > col) yub = col; end exub = xub-x; eyub = yub-y; exlb = x-xlb; eylb = y-ylb; outImage(i,j) = (exub*eyub*image(xlb,ylb))+(exlb*eyub*image(xub,ylb))+(exub*eylb*image(xlb,yub))+(exlb*eylb*image(xub,yub)); end end end My question is how can i modify the above code to get uncropped image ? I want to get following image
Try to see if this code produces the result you need: img = imread('aSxLS.png'); scale = [ 2 0 0 0 1 0 0 0 1 ]; tform = maketform('affine',inv(scale)); img_tform = imtransform(img,tform,'bilinear'); figure(); imshow(img); figure(); imshow(img_tform);
why I get max_weights=1 after gradient check?
I set a 3 layers neural network,it has 2 hidden layers,But when I try to implement gradient check ,I got my max_weigh=1 ,which means I have some error in my backprop.here are my backprop function,I really need some help is there something wrong with my codes? thanks! def loss(self,X,y,reg = 0.0): #forward prop W1, b1 = self.params['W1'], self.params['b1'] W2, b2 = self.params['W2'], self.params['b2'] W3, b3 = self.params['W3'], self.params['b3'] N,D = X.shape H1out = np.maximum(0,X.dot(W1)+b1) #H1out (N,H1) H2out = np.maximum(0,H1out.dot(W2)+b2) #H2out (N,H2) scores = None scores = H2out.dot(W3)+b3 scores_shift = scores-np.max(scores,axis = 1).reshape(-1,1) softmaxout = np.exp(scores_shift)/np.sum(np.exp(scores_shift),axis=1).reshape(-1,1) loss_main = None loss = None loss_main = -np.sum(np.log(softmaxout[range(N),list(y)])) loss = loss_main/N + reg*np.sum(W1*W1)*np.sum( W2*W2)+np.sum(W3*W3) #backward prop dscores = softmaxout.copy() #dscores (N,C) dscores[range(N),list(y)] -= 1 dscores /= N dW3 = H2out.T.dot(dscores) db3 = np.sum(dscores,axis = 0) dh2 = dscores.dot(W3.T) #dh2 (N,H2) dh_Relu2 = (H2out > 0) * dh2 #dh_ReLu2 (N,H2) dW2 = H1out.T.dot(dh_Relu2) db2 = np.sum(dh_Relu2,axis = 0) dh1 = dh_Relu2.dot(W2.T) #dh1 (N,H1) dh_Relu1 = (H1out>0) * dh1 dW1 = X.T.dot(dh_Relu1) db1 = np.sum(dh_Relu1,axis = 0) grad = {} grad['W1'] = dW1 grad['b1'] = db1 grad['W2'] = dW2 grad['b2'] = db2 grad['W3'] = dW3 grad['b3'] = db3 return loss,grad
value in range for big datasets
I have a problem that I can't seem to solve. I want a query to determine whether a given value lies within a predefined range, but my loop is very slow for big datasets. Is there a more efficient way? clear all close all Regression(1,1) = 1.001415645694801; Regression(1,2) = 0.043822386790753; FF_Value(:,1) = [24.24 30.77 31.37 29.05 29.20 29.53 29.67 27.78]; FF_Value(:,2) = [24.16 30.54 31.15 29.53 29.39 29.34 29.53 28.17]; FF_Distance = FF_Value(:,2)-(Regression(1,2)+Regression(1,1)*FF_Value(:,1)); FF_Distance_Positiv = sort(FF_Distance(FF_Distance > 0)); FF_Distance_Positiv(FF_Distance_Positiv == 0) = []; FF_Distance_Negativ = sort(FF_Distance(FF_Distance < 0),'descend'); FF_Distance_Negativ(FF_Distance_Negativ == 0) = []; A = repmat(FF_Distance_Positiv,length(FF_Distance_Negativ),1); B = repmat(FF_Distance_Negativ',length(FF_Distance_Positiv),1); C = reshape(B,[length(FF_Distance_Positiv)*length(FF_Distance_Negativ),1]); Recognition(:,1) = A; Recognition(:,2) = C; FF_Recognition = zeros(length(FF_Value),1); for i = 1:length(Recognition) for j = 1:length(FF_Value) if (Regression(1,2)+Recognition(i,1))+Regression(1,1)*FF_Value(j,1) >= FF_Value(j,2) &&... (Regression(1,2)+Recognition(i,2))+Regression(1,1)*FF_Value(j,1) <= FF_Value(j,2) FF_Recognition(j,1) = 1; end end end
Welcome to the world of bsxfun's replacing your world of repmats - %------------ Original code ----------------------------------------- FF_Distance = FF_Value(:,2)-(Regression(1,2)+Regression(1,1)*FF_Value(:,1)); FF_Distance_Positiv = sort(FF_Distance(FF_Distance > 0)); FF_Distance_Positiv(FF_Distance_Positiv == 0) = []; %// Note for Performance: If number of elements satisfying `FF_Distance_Positiv == 0` %// is a lot, consider doing this instead - %// `FF_Distance_Positiv = FF_Distance_Positiv(FF_Distance_Positiv~=0)`. %// Follow this strategy for `FF_Distance_Negativ` too. FF_Distance_Negativ = sort(FF_Distance(FF_Distance < 0),'descend'); FF_Distance_Negativ(FF_Distance_Negativ == 0) = []; %------- Added vectorization replacing `repmats` and nested loops ------------ mult = Regression(1,1)*FF_Value(:,1); y1 = bsxfun(#plus,Regression(1,2),FF_Distance_Positiv); y2 = bsxfun(#plus,y1.',mult); %//' mc1 = bsxfun(#ge,y2,FF_Value(:,2)); z1 = bsxfun(#plus,Regression(1,2),FF_Distance_Negativ); z2 = bsxfun(#plus,z1.',mult); %//' mc2 = bsxfun(#le,z2,FF_Value(:,2)); FF_Recognition = all([any(mc1,2) any(mc2,2)],2);
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.