Tuesday, November 26, 2013

What Is The Collision Possibility Of Car Remote Control

What Is The Collision Possibility Of Car Remote Control


by cawan (cawan[at]ieee.org or chuiyewleong[at]hotmail.com)

on 26/11/2013


Assume there are a number of car remote controls which are operating in
315 MHz where each of them will occupy 50 ms for any button being
pressed. If each of the car remote control be pressed for every 1 second
randomly, then what is the number of the car remote control should
have to induce the collision possibility to 0.5 ? Well, that's almost similar
to birthday paradox which is the fundamental of ancient syn flood attack.

In one second (1000 ms), there are 1000/50 = 20 of 50 ms time slots.
While there are only 2 car remote controls, the possibility they don't
collide each other is 20/20 x 19/20. Yes, the possibility seems high.
However, while the number of car remote controls go up to 6, the
possibility they don't collide each other become

20/20 x 19/20 x 18/20 x 17/20 x 16/20 x 15/20 = 0.44

So, the possibility they will collide each other is 1 - 0.44 = 0.56. Thus,
the possibility for the 6 car remote controls will collide each other in
1 second is higher than 0.5. Well, this might not an issue at all because
while the first press has no response, just press the second time and it
should trigger. For those who looking for jamming the system, simply
fill all the time slots, it should work.


Saturday, November 9, 2013

Why An Angle Of 180° Can Be Represented By Pi (π) ?

Why An Angle Of 180° Can Be Represented By π ?


by cawan (cawan[at]ieee.org or chuiyewleong[at]hotmail.com)

on 09/11/2013


An angle can be represented in two units, degree (°) or radian (rad).  In the unit of degree, a full circle is 360°.  So, half circle is 180° and quarter circle is 90° (orthogonal).  However, in the unit of radian, 1 rad is defined as the arc of a circle has same length as the radius (j) of that circle.  We all know the perimeter of a full circle is 2πj.  So, a full circle is 2πj / j = 2π rad, a half circle 2π / 2 = π rad, and of course a quarter circle is π/2 rad.  Anyway, both of them are just a representation of an angle, nothing special.  When a an angle becomes a parameter of a trigonometry function such as cosine, then it makes more senses.  We know the cosine of an angle is the ratio of adjacent to the hypotenuse.  So, when an angle is near to 0 rad, the adjacent is almost equal to hypotenuse.  Thus, the cosine of 0 rad is 1.  While the angle start to increase, the adjacent will decrease accordingly.  When the angle is near to π/2 rad or 90° (quarter circle), the adjacent will close to 0.  Hence, the cosine of π/2 rad or 90° is 0.  If the angle keep increasing, the adjacent will start to increase again, but in opposite direction.  When the angle is near to π rad or 180° (half circle), the adjacent becomes almost equal to hypotenuse again.  So, the cosine of π rad or 180° is -1 (remember the adjacent is in opposite direction ?).  For the same reason, the cosine of 270° and 360° are 0 and 1, respectively.  If we plot the variation of cosine of an angle from 0° to 360° and keep repeating, then we get a standard cosine wave.  The variation of angle from 0° to 360° is defined as a cycle.  The variation speed of the angle is defined as angular frequency (ω) in the unit of radian per second, which concerning how many cycle can be completed in 1 second.  For this reason, a frequency in the unit of Hz can be expressed in radian per second by multiplying with 2π (remember 2π rad is equal to 360° ?).  Thus, ω = 2πf.  In addition, a cosine wave can be expressed in cos(ωt), where the t is the time axis.  Now, how about the sine of an angle ? Well, I will leave this as your own exercise.

Sunday, November 3, 2013

A Brief Discussion Of j and e From The Perspective Of Signal Processing

A Brief Discussion Of j and e From The Perspective Of Signal Processing


by cawan (cawan[at]ieee.org or chuiyewleong[at]hotmail.com)

on 03/11/2013


We all know about this equation





When we multiply an integer n with j, then it becomes nj.  If we multiply nj with j again, 
then it becomes –n.  Again, if we multiply –n with j, it becomes –nj.  If we multiply the
–nj with j again, then we get n.  So, if we keep multiplying an integer with j, then we 
get a repeating series, n, nj, -n, -nj, n, nj, -n, -nj, n,… 

If we assume the j is residing at imaginary axis, which is orthogonal to the real axis in a 
complex plane, then the repeating series can be plotted on the complex plane as a rotating 
circle in anticlockwise.  On the other hand, if the integer n keeps multiplying with –j 
(instead of +j), then the rotating circle is in clockwise.  Now, we need to prove the j is really 
orthogonal to the real axis.  If we define the real axis as x-axis and imaginary axis as y-axis, 
for any point located at the complex plane can be expressed as (x, y).  For the case of a circle 
on the complex plane, if a point (x, y) rotates an angle θ in anticlockwise, the new point 
(x1, y2) can be expressed as






On the other hand, if we represent (x, y) as a complex number, x+jy, and multiply it with 
e^(jθ) , then what will happen?  Well, Euler’s equation told us





So, 







Apparently, equation (5) is matching to equation (2) and (3).  Thus, we can say that now 
for any point (x, y) located at a complex plane, if the point rotates an angle θ in anticlockwise, 
then the new point (x1, x2) can be obtained by multiplying (x, y) with e^(jθ) .  For the same 
reason, if a point at real axis rotates 90° in anticlockwise, then the point should multiply with 
e^(j(π/2)) in order to get to the new point.  If we express e^(j(π/2)) in polar form, then









Well, now there is no issue for imaginary axis is always orthogonal to the real axis.  However, 
while a point keep rotating as a circle in a complex plane, then the number of cycle being 
completed for a full circle in one second is defined as frequency in the unit of radians per 
second, ω.  So,













Where the unit of f  is in Hz.  So now, 







From (10) and (11), we know that a complex sinusoidal signal will comprise of two sinusoidal 
signals in same frequency but with 90° of phase difference.  However, in practical, we normally 
deal with real number signal, instead of complex signal.  For the case of cos(ωt), it can be expressed 
in exponential form.






From (12), it shows a real number frequency will comprise of a positive and a negative complex
frequencies, which is well known as even symmetry of magnitude spectrum.  If there is a signal 
with center frequency at f1 multiply with cos(2πf1t), then we can two signals with different center
frequency, 2f1 and 0.  So, if we use a low pass filter to remove 2f1 from the product signal, then 
we can get a signal with center frequency at 0 Hz.  In software-defined radio (SDR), such approach 
is useful for analog to digital converter (ADC) to sample high frequency signal.  Now, how about 
sin(ωt)?  Well, it can be expressed in exponential form too.






From (13), it shows for any signal which multiply with sin(ωt), it will be turned into imaginary axis
(because it is multiplying with j).  As similar to cos(ωt), sin(ωt) also comprises of a positive and a 
negative frequencies.  However, the positive frequency would have a negative magnitude spectrum as 
oppose to negative frequency, which is well known as odd symmetry.  When we multiply a signal with 
sin(ωt) where the ω is equal to the center frequency of the signal, we can bring the signal to 0 Hz as 
center frequency.  In additional, since there is a j come together with sin(ωt), while multiplying the 
signal with sin(ωt), the spectrum of the signal with 0 Hz as center frequency is actually located at the 
imaginary axis.  As we know, the process of quadrature sampling is about to transform a real number 
signal into a complex number signal.  Thus, by multiplying a real number signal with cos(ωt) and 
sin(ωt) in individual, after being processed by low pass filter, we get Re(t) and Im(t), respectively.  
The Re(t) and Im(t) are sometimes defined as i(t) and q(t), respectively, where the i stand for in-phase 
and the q stand for quadrature.  Besides, both of them having half magnitude at center frequency of 0 
Hz (we assume the original center frequency of the signal is identical to the ω).  However, Im(t) is at 
imaginary axis, which is orthogonal to the real axis, so, we need to twist it 90° in clockwise by 
multiplying with -j to make it aligned to real axis.  Whiles both of them with half magnitude are 
superposed in real axis, they construct a full magnitude real number signal with 0 Hz as center 
frequency.  As a result, the complex number signal of a real number signal x(t) can be expressed as 
x(t)cos(ωt) -jx(t)sin(ωt).  Someone might argue the x(t) should have its negative spectrum as it is a 
real number signal, and we didn’t consider about the impact of positive spectrum of cos(ωt) and
sin(ωt) yet.  Well, I leave that part as your own exercise to show the signals cancellation among them.  

Wednesday, October 23, 2013

How To Reverse The Order Of A Four Bytes Integer In Python

How To Reverse The Order Of A Four Bytes Integer In Python


by cawan (cawan[at]ieee.org or chuiyewleong[at]hotmail.com)

on 23/10/2013


Just a simple reminder for those who new to python. Instead of using bit wise operation in accomplishing a logical function such as reversing the order of a four bytes integer, it is better to make use to the library which has such identical function. For the case of reversing the order of a four bytes integer, instead of using



it is simpler and easier to use



as alternative.

Sunday, October 20, 2013

How To Cross Compile A Binary For iDevices

How To Cross Compile A Binary For iDevices

by cawan (cawan[at]ieee.org or chuiyewleong[at]hotmail.com)

on 20/10/2013

The gcc that comes with OSX without Xcode being installed does not include cross
compilation toolchain in generating software for idevices. Let's verify.

awans-MacBook-Pro:~ cawan$ gcc
i686-apple-darwin11-llvm-gcc-4.2: no input files
cawans-MacBook-Pro:~ cawan$ which gcc
/usr/bin/gcc
cawans-MacBook-Pro:~ cawan$ ls -l /usr/bin/gcc
lrwxr-xr-x  1 root  wheel  12 Sep 26  2012 /usr/bin/gcc -> llvm-gcc-4.2
cawans-MacBook-Pro:~ cawan$ ls -l /usr/bin/llvm-gcc-4.2
lrwxr-xr-x  1 root  wheel  32 Sep 26  2012 /usr/bin/llvm-gcc-4.2 ->
../llvm-gcc-4.2/bin/llvm-gcc-4.2
cawans-MacBook-Pro:~ cawan$ ls -l /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
-rwxr-xr-x  1 root  wheel  117168 Sep 26  2012 /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2

So, the gcc is just a symbolic link to llvm-gcc-4.2. Let's check the llvm-gcc-4.2.

awans-MacBook-Pro:~ cawan$ /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
i686-apple-darwin11-llvm-gcc-4.2: no input files
cawans-MacBook-Pro:~ cawan$ /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -arch armv7
llvm-gcc-4.2: error trying to exec '/usr/llvm-gcc-4.2/bin/arm-apple-darwin11-llvm-gcc-4.2':
execvp: No such file or directory
cawans-MacBook-Pro:~ cawan$ /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -arch i686
i686-apple-darwin11-llvm-gcc-4.2: no input files

Well, the llvm-gcc-4.2 is just a forwarder to an appropriate compiler, either
i686-apple-darwin11-llvm-gcc-4.2 or arm-apple-darwin11-llvm-gcc-4.2, which is
decided by "-arch" parameter. When "-arch" is not specified, the default is
i686-apple-darwin11-llvm-gcc-4.2. Now, let us install a copy of Xcode (I am using
Xcode 4.5 here). After the installation is completed, the llvm-gcc-4.2 can be
found in 2 locations.

cawans-MacBook-Pro:bin cawan$ pwd
/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/bin
cawans-MacBook-Pro:bin cawan$ ls -l
total 1464
-rwxr-xr-x@ 1 cawan  admin  127776 Sep  8  2012 gcov-4.2
-rwxr-xr-x@ 1 cawan  admin  544416 Sep  8  2012 i686-apple-darwin11-llvm-g++-4.2
-rwxr-xr-x@ 1 cawan  admin  544416 Sep  8  2012 i686-apple-darwin11-llvm-gcc-4.2
-rwxr-xr-x@ 1 cawan  admin  117168 Aug  5  2012 llvm-c++-4.2
-rwxr-xr-x@ 1 cawan  admin  257552 Sep  8  2012 llvm-cpp-4.2
-rwxr-xr-x@ 1 cawan  admin  117168 Sep  8  2012 llvm-g++-4.2
-rwxr-xr-x@ 1 cawan  admin  117168 Sep  8  2012 llvm-gcc-4.2
cawans-MacBook-Pro:bin cawan$

cawans-MacBook-Pro:bin cawan$ pwd
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/llvm-gcc-4.2/bin
cawans-MacBook-Pro:bin cawan$ ls -l
total 2344
-rwxr-xr-x@ 1 cawan  admin  555104 Sep  8  2012 arm-apple-darwin10-llvm-g++-4.2
-rwxr-xr-x@ 1 cawan  admin  546896 Sep  8  2012 arm-apple-darwin10-llvm-gcc-4.2
-rwxr-xr-x@ 1 cawan  admin  130816 Sep  8  2012 gcov-4.2
-rwxr-xr-x@ 1 cawan  admin  551008 Sep  8  2012 i686-apple-darwin10-llvm-g++-4.2
-rwxr-xr-x@ 1 cawan  admin  546896 Sep  8  2012 i686-apple-darwin10-llvm-gcc-4.2
-rwxr-xr-x@ 1 cawan  admin  106112 Sep  8  2012 llvm-c++-4.2
-rwxr-xr-x@ 1 cawan  admin  255728 Sep  8  2012 llvm-cpp-4.2
-rwxr-xr-x@ 1 cawan  admin  106112 Sep  8  2012 llvm-g++-4.2
-rwxr-xr-x@ 1 cawan  admin  106112 Sep  8  2012 llvm-gcc-4.2
cawans-MacBook-Pro:bin cawan$

So, the cross compiler is only available in Platforms/iPhoneOS.platform/Developer/.
Now, let us try to build a simple binary to run in idevice.

cawans-MacBook-Pro:bin cawan$ ./llvm-gcc-4.2
i686-apple-darwin10-llvm-gcc-4.2: no input files
cawans-MacBook-Pro:bin cawan$ ./llvm-gcc-4.2 -arch armv7
arm-apple-darwin10-llvm-gcc-4.2: no input files
cawans-MacBook-Pro:bin cawan$ cat cawan_hello.c
#include
main()
{
  printf("\nhello cawan\n");
}
cawans-MacBook-Pro:bin cawan$ ./llvm-gcc-4.2 -arch armv7 cawan_hello.c -o cawan_hello
In file included from /usr/include/sys/_types.h:33,
                 from /usr/include/_types.h:27,
                 from /usr/include/stdio.h:67,
                 from cawan_hello.c:1:
/usr/include/machine/_types.h:34:24: error: arm/_types.h: No such file or directory
In file included from /usr/include/_types.h:27,
                 from /usr/include/stdio.h:67,
                 from cawan_hello.c:1:
...
...
...
cawans-MacBook-Pro:bin cawan$

Well, the headers and libraries of idevice should be specified for the cross compilation
process. It is located in the directory of iphone SDK being installed together with Xcode.

cawans-MacBook-Pro:bin cawan$ ls -l /Applications/Xcode.app/Contents/Developer/Platforms/
iPhoneOS.platform/Developer/SDKs/
total 0
drwxr-xr-x@  8 cawan  admin  272 Oct 17 09:28 iPhoneOS5.1.sdk
drwxr-xr-x@ 10 cawan  admin  340 Oct 15 23:47 iPhoneOS6.0.sdk
cawans-MacBook-Pro:bin cawan$

I have 2 versions of SDKs installed, 5.1 and 6.0. Now, I am going to use version 6.0.

cawans-MacBook-Pro:bin cawan$ ./llvm-gcc-4.2 -arch armv7 -o cawan_hello -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/
iPhoneOS6.0.sdk cawan_hello.c
cawans-MacBook-Pro:bin cawan$ ls -l cawan_hello
-rwxr-xr-x  1 cawan  admin  12604 Oct 20 15:13 cawan_hello
cawans-MacBook-Pro:bin cawan$ file cawan_hello
cawan_hello: Mach-O executable arm
cawans-MacBook-Pro:bin cawan$ lipo -info cawan_hello
Non-fat file: cawan_hello is architecture: armv7
cawans-MacBook-Pro:bin cawan$

Fine, it seems the binary is ready. Let's run it in iphone environment. Turn on the ssh
server in iphone and use tcprelay to connect into the iphone.

cawans-MacBook-Pro:python-client cawan$ python tcprelay.py 22:2222
Forwarding local port 2222 to remote port 22

Now, it is ready to copy the cawan_hello into the iphone via port 2222.

cawans-MacBook-Pro:bin cawan$ scp -P 2222 cawan_hello root@127.0.0.1:/Applications/
root@127.0.0.1's password:
cawan_hello                                   100%   12KB  12.3KB/s   00:00  
cawans-MacBook-Pro:bin cawan$

The cawan_hello is copied into /Applications/ of the iphone. So, it is ready to connect
into the iphone and run the cawan_hello.

cawans-MacBook-Pro:bin cawan$ ssh -p 2222 root@127.0.0.1
root@127.0.0.1's password:
snows-iPhone:~ root# cd /Applications
snows-iPhone:/Applications root# ls cawan_hello
cawan_hello*
snows-iPhone:/Applications root# ./cawan_hello
-sh: ./cawan_hello: Bad CPU type in executable
snows-iPhone:/Applications root#

Unfortunately, it seems the processor architecture of my iphone is not armv7. From the
back cover of my iphone, I can see its model number is A1241. Well, it is iphone 3G and
its processor architecture is armv6, not armv7. Let's recompile the binary again.

cawans-MacBook-Pro:bin cawan$ ./llvm-gcc-4.2 -arch armv6 -o cawan_hello -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/
iPhoneOS6.0.sdk cawan_hello.c
ld: file is universal (4 slices) but does not contain a(n) armv6 slice:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/
iPhoneOS6.0.sdk/usr/lib/crt1.o for architecture armv6
collect2: ld returned 1 exit status
cawans-MacBook-Pro:bin cawan$

Well, it seems iphone sdk 6.0 is not compatible with armv6 architecture. Let's use
iphone sdk 5.1 instead.

cawans-MacBook-Pro:bin cawan$ ./llvm-gcc-4.2 -arch armv6 -o cawan_hello -isysroot
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/
SDKs/iPhoneOS5.1.sdk cawan_hello.c
cawans-MacBook-Pro:bin cawan$

Copy the new cawan_hello into the iphone and run it again.

cawans-MacBook-Pro:bin cawan$ scp -P 2222 cawan_hello root@127.0.0.1:/Applications/
root@127.0.0.1's password:
cawan_hello                                   100%   12KB  12.3KB/s   00:00  
cawans-MacBook-Pro:bin cawan$ ssh -p 2222 root@127.0.0.1
root@127.0.0.1's password:
snows-iPhone:~ root# cd /Applications
snows-iPhone:/Applications root# ls cawan_hello
cawan_hello*
snows-iPhone:/Applications root# ./cawan_hello
Killed
snows-iPhone:/Applications root#

Good, the cawan_hello is capable to run in my iphone but it is killed by the iOS.
This is because the iOS will verify the signature of a binary first before allowing
it to run. If the signiture verification check is failed, then the iOS will kill
the binary which attempting to run. Anyway, since I know my iphone is jailbroken,
so I can simply use ldid to perform pseudo-signing to my cawan_hello. Let's try again.

cawans-MacBook-Pro:bin cawan$ ldid -S cawan_hello
cawans-MacBook-Pro:bin cawan$ scp -P 2222 cawan_hello root@127.0.0.1:/Applications/
root@127.0.0.1's password:
cawan_hello                                   100%   13KB  12.5KB/s   00:00  
cawans-MacBook-Pro:bin cawan$ ssh -p 2222 root@127.0.0.1
root@127.0.0.1's password:
snows-iPhone:~ root# cd /Applications
snows-iPhone:/Applications root# ls cawan_hello
cawan_hello*
snows-iPhone:/Applications root# ./cawan_hello

hello cawan
snows-iPhone:/Applications root#

Excellent, the cawan_hello can run properly in my iphone now. There are some arguments
about is it possible for Xcode 4.5 in generating binary for armv6 based idevices such
as iphone 3G or iPod touch 2G. From this exercise, it is proven the Xcode 4.5 is able
to generate armv6 binary by specifiying the base sdk as 5.1 and target architecture as
armv6.

pdf version:

http://www.scribd.com/doc/177494010/How-to-Cross-Compile-a-Binary-for-iDevices

Wednesday, October 9, 2013

What is Immersion Gold ?

What is Immersion Gold ?

by cawan (cawan[at]ieee.org or chuiyewleong[at]hotmail.com)

on 09/10/2013


Before a blank PCB is ready for PCBA, if there are a lot of extra small scale SMDs being used
such as 0603, 0402, 0201..., or high density BGA, then it is recommended for immersion gold
to the blank PCB. The main objective of immersion gold is to provide better contactness for
those SMDs to the pads, in order to reduce the failure rate in mass production. Of course this
will induce extra production cost, but the impact is significant. For those who can't tolerate
to the extra cost, consider to immersion silver or stick to immersion tin instead.