I am trying to validate EAN 14 UPC code in vba access. I am trying to find it online but no luck. I just found for EAN 8 and EAN 13. So, I just tried to code it similar to EAN 13 as following:
If Len(Barcode) = 14 Then
'do the check digit for EAN 14 for anything 14 long
checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
+ (Val(Mid(Barcode, 4, 1))) _
+ (Val(Mid(Barcode, 6, 1))) _
+ (Val(Mid(Barcode, 8, 1))) _
+ (Val(Mid(Barcode, 10, 1))) _
+ (Val(Mid(Barcode, 12, 1)))
checkDigitSubtotal = (3 * checkDigitSubtotal) _
+ (Val(Mid(Barcode, 1, 1))) _
+ (Val(Mid(Barcode, 3, 1))) _
+ (Val(Mid(Barcode, 5, 1))) _
+ (Val(Mid(Barcode, 7, 1))) _
+ (Val(Mid(Barcode, 9, 1))) _
+ (Val(Mid(Barcode, 11, 1))) _
+ (Val(Mid(Barcode, 13, 1)))
If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
Validate_UPC = "EAN14-BAD"
Exit Function
End If
Validate_UPC = "EAN14-GOOD"
Exit Function
End If
It is not working. Issue I am having is although i enter valid EAN, it gives me EAN14-BAD. I think my validating code is not working. I just added last line
+ (Val(Mid(Barcode, 13, 1)))
on EAN13 validation code. Please help.
It worked when I switched multiplying odd ones by 3 as following:
If Len(Barcode) = 14 Then
checkDigitSubtotal = (Val(Mid(Barcode, 1, 1))) _
+ (Val(Mid(Barcode, 3, 1))) _
+ (Val(Mid(Barcode, 5, 1))) _
+ (Val(Mid(Barcode, 7, 1))) _
+ (Val(Mid(Barcode, 9, 1))) _
+ (Val(Mid(Barcode, 11, 1))) _
+ (Val(Mid(Barcode, 13, 1)))
checkDigitSubtotal = (3 * checkDigitSubtotal) _
+ (Val(Mid(Barcode, 2, 1))) _
+ (Val(Mid(Barcode, 4, 1))) _
+ (Val(Mid(Barcode, 6, 1))) _
+ (Val(Mid(Barcode, 8, 1))) _
+ (Val(Mid(Barcode, 10, 1))) _
+ (Val(Mid(Barcode, 12, 1)))
If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
Validate_UPC = "EAN14-BAD"
Exit Function
End If
Validate_UPC = "EAN14-GOOD"
Exit Function
End If
Did you try with 8 characters?
If Len(Barcode) = 8 Then
'do the check digit for EAN 8 for anything 8 long
checkDigitSubtotal = (Val(Mid(Barcode, 2, 1))) _
+ (Val(Mid(Barcode, 4, 1))) _
+ (Val(Mid(Barcode, 6, 1)))
checkDigitSubtotal = (3 * checkDigitSubtotal) _
+ (Val(Mid(Barcode, 1, 1))) _
+ (Val(Mid(Barcode, 3, 1))) _
+ (Val(Mid(Barcode, 5, 1))) _
+ (Val(Mid(Barcode, 7, 1)))
If Right(Str(300 - checkDigitSubtotal), 1) <> Right(Barcode, 1) Then
Validate_UPC = "EAN8-BAD"
Exit Function
End If
Validate_UPC = "EAN8-GOOD"
Exit Function
End If
EAN8 AND EAN13 IN VB.NET
Public Function generateEAN(ByVal barcode As String, EsEan8 As Boolean) As String
Dim first As Integer = 0
Dim second As Integer = 0
If EsEan8 Then
barcode = Right(("00000000" & barcode), 7)
Else
barcode = Right(("000000000000" & barcode), 12)
End If
If barcode.Length() = 7 OrElse barcode.Length() = 12 Then
For counter As Integer = 0 To barcode.Length() - 1 Step 2
first = (first + barcode.Substring(counter, 1))
If counter + 1 < barcode.Length Then
second = (second + barcode.Substring(counter + 1, 1))
End If
Next
If EsEan8 Then
first = first * 3
Else
second = second * 3
End If
Dim total As Integer = second + first
Dim roundedNum As Integer = (10 - (total Mod 10)) Mod 10
barcode = barcode & roundedNum
End If
Return barcode
End Function
starting from this code:
clc, clear all, close all
tic
k1 = 0.01:0.1:100;
k2 = 0.01:0.1:100;
k3 = 0.01:0.1:100;
k = sqrt(k1.^2 + k2.^2 + k3.^2);
c = 1.476;
gamma = 3.9;
colors = {'cyan'};
Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E = #(k) (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = zeros(1,numel(k1));
E_int(1) = 1.5;
for i = 2:numel(k)
E_int(i) = E_int(i-1) - integral(E,k(i-1),k(i));
end
beta = c*gamma./(k.*sqrt(E_int));
F_11 = zeros(1,numel(k1));
F_22 = zeros(1,numel(k1));
F_33 = zeros(1,numel(k1));
count = 0;
for i = 1:numel(k1)
count = count + 1;
phi_11 = #(k2,k3) phi_11_new(k1,k2,k3,beta,i);
phi_22 = #(k2,k3) phi_22_new(k1,k2,k3,beta,i);
phi_33 = #(k2,k3) phi_33_new(k1,k2,k3,beta,i);
F_11(count) = integral2(phi_11,-100,100,-100,100);
F_22(count) = integral2(phi_22,-100,100,-100,100);
F_33(count) = integral2(phi_33,-100,100,-100,100);
end
figure
hold on
plot(k1,F_11,'b')
plot(k1,F_22,'cyan')
plot(k1,F_33,'magenta')
hold off
where
function phi_11 = phi_11_new(k1,k2,k3,beta,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
k30 = k3 + beta(i).*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta(i).*k1(i).^2).*(k1(i).^2 + k2.^2 - k3.*k30)./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta(i).*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta(i)));
xhsi1 = C1 - k2./k1(i).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1(i).^2 - 2.*k1(i).*k30.*xhsi1 + (k1(i).^2 + k2.^2).*xhsi1_q);
end
function phi_22 = phi_22_new(k1,k2,k3,beta,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
k30 = k3 + beta(i).*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta(i).*k1(i).^2).*(k1(i).^2 + k2.^2 - k3.*k30)./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta(i).*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta(i)));
xhsi2 = k2./k1(i).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1(i).^2 + k2.^2).*xhsi2_q);
end
function phi_33 = phi_33_new(k1,k2,k3,beta,i)
k = sqrt(k1(i).^2+k2.^2+k3.^2);
k30 = k3 + beta(i).*k1(i);
k0 = sqrt(k1(i).^2+k2.^2+k30.^2);
E_k0 = (1.453.*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi.*(k.^4))).*(k1(i).^2+k2.^2);
end
This procedure is leading me to results not matching some others coming from a study. The results I should match are posted below:
whereas mine look like these
It's quite easy to esteem how only the comp w match the theoretical results; therefore, I believe that the flaw may reside in the definition of beta outside the function phi_11_new (and phi_22_new).
May any of you suggest how to calculate beta within phi_11_new(and phi_22_new) instead than the way I currently do?
I thank you all in advance for supporting.
Best Regards,
fpe
I have improved the interpolation so that it no longer breaks down for small values. It also returns more correct values since it now interpolates the logarithms of the values.
Here is the code, as it is now.
function test15()
[k1,k2,k3] = deal(0.01:0.1:400);
k = sqrt(k1.^2 + k2.^2 + k3.^2);
c = 1.476;
gamma = 3.9;
Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = 1.5-cumtrapz(k,Ek);
beta = c*gamma./(k.*sqrt(E_int));
[F_11,F_22,F_33] = deal(zeros(1,numel(k1)));
k_vec = k;
beta_vec = beta;
kLim = 100;
for ii = 1:numel(k1)
phi_11 = #(k2,k3) phi_11_new(k1(ii),k2,k3,k_vec,beta_vec);
phi_22 = #(k2,k3) phi_22_new(k1(ii),k2,k3,k_vec,beta_vec);
phi_33 = #(k2,k3) phi_33_new(k1(ii),k2,k3,k_vec,beta_vec);
F_11(ii) = quad2d(phi_11,-kLim,kLim,-kLim,kLim);
F_22(ii) = quad2d(phi_22,-kLim,kLim,-kLim,kLim);
F_33(ii) = quad2d(phi_33,-kLim,kLim,-kLim,kLim);
end
figure
loglog(k1,F_11,'b')
hold on
loglog(k1,F_22,'cyan')
loglog(k1,F_33,'magenta')
hold off
grid on
end
function phi_11 = phi_11_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);
log_beta_vec = interp1(log(k_vec),log(beta_vec),log(k(:)),'linear','extrap');
log_beta = reshape(log_beta_vec,size(k));
beta = exp(log_beta);
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30*k1.*beta));
xhsi1 = C1 - (k2/k1).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1^2 - 2*k1*k30.*xhsi1 + (k1^2 + k2.^2).*xhsi1_q);
end
function phi_22 = phi_22_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);
log_beta_vec = interp1(log(k_vec),log(beta_vec),log(k(:)),'linear','extrap');
log_beta = reshape(log_beta_vec,size(k));
beta = exp(log_beta);
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30.*k1.*beta));
xhsi2 = (k2/k1).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1^2 + k2.^2).*xhsi2_q);
end
function phi_33 = phi_33_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2+k2.^2+k3.^2);
log_beta_vec = interp1(log(k_vec),log(beta_vec),log(k(:)),'linear','extrap');
log_beta = reshape(log_beta_vec,size(k));
beta = exp(log_beta);
k30 = k3 + beta*k1;
k0 = sqrt(k1^2+k2.^2+k30.^2);
E_k0 = (1.453*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi*(k.^4))).*(k1^2+k2.^2);
end
The figure seems to agree with the original result quite well. Even if there still are some differences.
Side note: Since a k-value of 100 is set as an upper limit in the simulation the values greater than this in the figure are incorrect. They are calculated without using all values in the full (k2,k3)-"circle". We can also see a deviation for these values.
Ok, this is what I have got at the moment. I would like to hear what you think about it - it is not perfect yet. I have not access to the functions integral or integral2 so if you can reinsert them (instead of my quad2d for example) and test the code you might get better results than I have now.
My first thought was to calculate beta in a for-loop for every triplet of [k1,k2,k3] in the phi-functions. This turned out to be extremely slow so I have instead used a vector of k-values and calculated the corresponding vector beta just as you did before. These two vectors are then passed to phi where the values are used in an interpolation function (interp1) to find the beta-values of specific k-values.
function myFunction()
[k1,k2,k3] = deal(0.01:0.1:400);
k = sqrt(k1.^2 + k2.^2 + k3.^2);
c = 1.476;
gamma = 3.9;
Ek = (1.453*k.^4)./((1 + k.^2).^(17/6));
E_int = 1.5-cumtrapz(k,Ek);
beta = c*gamma./(k.*sqrt(E_int));
[F_11,F_22,F_33] = deal(zeros(1,numel(k1)));
k_vec = k;
beta_vec = beta;
for ii = 1:numel(k1)
phi_11 = #(k2,k3) phi_11_new(k1(ii),k2,k3,k_vec,beta_vec);
phi_22 = #(k2,k3) phi_22_new(k1(ii),k2,k3,k_vec,beta_vec);
phi_33 = #(k2,k3) phi_33_new(k1(ii),k2,k3,k_vec,beta_vec);
F_11(ii) = quad2d(phi_11,-100,100,-100,100);
F_22(ii) = quad2d(phi_22,-100,100,-100,100);
F_33(ii) = quad2d(phi_33,-100,100,-100,100);
end
figure
loglog(k1,F_11,'b')
hold on
loglog(k1,F_22,'cyan')
loglog(k1,F_33,'magenta')
hold off
grid on
end
function phi_11 = phi_11_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);
beta = reshape(interp1(k_vec,beta_vec,k(:)),size(k));
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30*k1.*beta));
xhsi1 = C1 - (k2/k1).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1^2 - 2*k1*k30.*xhsi1 + (k1^2 + k2.^2).*xhsi1_q);
end
function phi_22 = phi_22_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2 + k2.^2 + k3.^2);
beta = reshape(interp1(k_vec,beta_vec,k(:)),size(k));
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30.*k1.*beta));
xhsi2 = (k2/k1).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1^2 + k2.^2).*xhsi2_q);
end
function phi_33 = phi_33_new(k1,k2,k3,k_vec,beta_vec)
k = sqrt(k1^2+k2.^2+k3.^2);
beta = reshape(interp1(k_vec,beta_vec,k(:)),size(k));
k30 = k3 + beta*k1;
k0 = sqrt(k1^2+k2.^2+k30.^2);
E_k0 = (1.453*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi*(k.^4))).*(k1^2+k2.^2);
end
This produces the following figure. Note that the integration does not succeed for the smallest values of k1.
Edit - A comment on calculating beta within the phi-functions
Since you essentially tried the same thing that I did initially, I have added an example of how I calculated the beta matrix within the phi-functions. Note that this code is so slow that I have never actually run it to completion.
function phi_11 = phi_11_new(k1,k2,k3)
k = sqrt(k1^2 + k2.^2 + k3.^2);
c = 1.476;
gamma = 3.9;
beta = zeros(size(k));
E = #(x) (1.453*x.^4)./((1 + x.^2).^(17/6));
for ii = 1:size(k,1)
for jj = 1:size(k,2)
E_int = 1.5-quad(E,0.001,k(ii,jj));
beta(ii,jj) = c*gamma/(k(ii,jj)*sqrt(E_int));
end
end
k30 = k3 + beta*k1;
k0 = sqrt(k1^2 + k2.^2 + k30.^2);
E_k0 = 1.453*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta*k1^2).*(k1^2 + k2.^2 - k3.*k30)./(k.^2.*(k1^2 + k2.^2));
C2 = k2.*k0.^2./((k1^2 + k2.^2).^(3/2)).*atan2((beta*k1.*sqrt(k1^2 + k2.^2)),(k0.^2 - k30*k1.*beta));
xhsi1 = C1 - (k2/k1).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1^2 - 2*k1*k30.*xhsi1 + (k1^2 + k2.^2).*xhsi1_q);
end
currently I'm running a different version of the above code.
It follows as
clc, clear all, close all
tic
k1 = (0.01:0.1:100);
c = 1.476;
gamma = 3.9;
F_11 = zeros(1,numel(k1));
F_22 = zeros(1,numel(k1));
F_33 = zeros(1,numel(k1));
count = 0;
for i = 1:numel(k1)
count = count + 1;
phi_11 = #(k2,k3) phi_11_new(k1,k2,k3,gamma,i);
phi_22 = #(k2,k3) phi_22_new(k1,k2,k3,gamma,i);
phi_33 = #(k2,k3) phi_33_new(k1,k2,k3,gamma,i);
F_11(count) = integral2(phi_11,-100,100,-100,100);
F_22(count) = integral2(phi_22,-100,100,-100,100);
F_33(count) = integral2(phi_33,-100,100,-100,100);
end
This time phi_11, phi_22 and phi_33 are given as
function phi_11 = phi_11_new(k1,k2,k3,gamma,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
beta = gamma./((k.^(2/3)).*sqrt(hypergeom([1/3,17/6],4/3,-k.^(-2))));
k30 = k3 + beta.*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta(i).*k1(i).^2).*(k0.^2 - 2.*k30.^2 + beta.*k30.*k1(i))./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta.*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta));
xhsi1 = C1 - k2./k1(i).*C2;
xhsi1_q = xhsi1.^2;
phi_11 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k1(i).^2 - 2.*k1(i).*k30.*xhsi1 + (k1(i).^2 + k2.^2).*xhsi1_q);
end
function phi_22 = phi_22_new(k1,k2,k3,gamma,i)
k = sqrt(k1(i).^2 + k2.^2 + k3.^2);
beta = gamma./((k.^(2/3)).*sqrt(hypergeom([1/3,17/6],4/3,-k.^(-2))));
k30 = k3 + beta.*k1(i);
k0 = sqrt(k1(i).^2 + k2.^2 + k30.^2);
E_k0 = 1.453.*k0.^4./((1 + k0.^2).^(17/6));
C1 = (beta.*k1(i).^2).*(k0.^2 - 2.*k30.^2 + beta.*k1(i).*k30)./(k.^2.*(k1(i).^2 + k2.^2));
C2 = k2.*k0.^2./((k1(i).^2 + k2.^2).^(3/2)).*atan2((beta.*k1(i).*sqrt(k1(i).^2 + k2.^2)),(k0.^2 - k30.*k1(i).*beta));
xhsi2 = (k2./k1(i)).*C1 + C2;
xhsi2_q = xhsi2.^2;
phi_22 = E_k0./(4.*pi.*k0.^4).*(k0.^2 - k2.^2 - 2.*k2.*k30.*xhsi2 + (k1(i).^2 + k2.^2).*xhsi2_q);
end
function phi_33 = phi_33_new(k1,k2,k3,gamma,i)
k = sqrt(k1(i).^2+k2.^2+k3.^2);
beta = gamma./((k.^(2/3)).*sqrt(hypergeom([1/3,17/6],4/3,-k.^(-2))));
k30 = k3 + beta.*k1(i);
k0 = sqrt(k1(i).^2+k2.^2+k30.^2);
E_k0 = (1.453.*k0.^4./((1+k0.^2).^(17/6)));
phi_33 = (E_k0./(4*pi.*(k.^4))).*(k1(i).^2+k2.^2);
end
Please notice that now beta is calculated within the phi functions. Furthermore, I'm using an equivalent expression of beta. For more details, check the picture below out
Hence, in the updated model, I'm calling hypergeom which is rather slow performing.
That's the main reason why I'd like to calculate beta as in the first code, by means of the energy spectrum integral.
Btw, at the moment I have no idea how to perfrom this successfully.
Best regards,
fpe