Friday, 16 November 2012

Huffman Coding in Python

Here is a simple explanation for the code to encode and decode the string which you have entered by using Huffman data compression. Huffman coding is a lossless data compression based on variable-length code table for encoding a source symbol where the variable-length code table has been derived in a particular way based on the estimated probability of occurrence for each possible value of the source symbol.
                          Huffman coding uses a specific method for choosing the representation for each symbol, resulting in a prefix code that expresses the most common source symbols using shorter strings of bits that are used for less common source symbols.
    The main part of code is the tree where on each instance creates a leaf node or the internal node which depends on the parameters you are passing.
  Here I have created a tree data structure which as a value named "cargo" then a "left" and a "right" which will be "None" when the node is a leaf node.

class Tree:
  def __init__(self, cargo, left=None, right=None):
    self.cargo = cargo
    self.left  = left
    self.right = right

The creation of tree is a bottom up procedure by starting with leaf nodes
left=Tree(1)
right=Tree(2) 
Then we can assign an intermediate node by using left and right as the second and third argument for Class Tree
Tree(1,left,right)
This Tree structure can be called recursively to get a multilevel binary tree
 Tree(1,Tree(1),Tree(2))
Here is an step by step description for the huffman code:

Step  1: First create a Tree data structure as explained earlier

Step  2: Read the string character by character and create a List of tuples which   contains the character and its number of occurrence which is its weight.

Step  3: The tuples can be made the leaf node by creating instance for each tuple without specifying the left and right parameters.

Step  4: After creating leaf node sort list of nodes with weights as the key. This can be done by using sorted method passing key as second element of the tuple

Step  5: Pop least two elements (tree) and add the value and the concatenate the characters.

Step  6: Again sort to get the least two elements.

Step  7: Repeat the process from 4 to 6 until all the characters without repetition are received

By previous 7 steps we have created the Tree for the given string. Now by using the Tree we can traverse to encode the string.

Algorithm for function encode

Step  1: Pass the tree, empty string and each characters to the function
Step  2: Check weather the code is in the root if "yes" then check the left or right node traverse the tree as per the condition gets true until reaches the leaf node.

Step  3: For the left node traversal concatenate the empty string with "0" and for the right concatenate "1"

Step  4: The functions returns the code when reaches the leaf node.

Algorithm for function decode:

Step  1: Pass the tree, the code and the empty string to the function.

Step  2: The copy for the tree is created so that after finding each characters then the tree can be reassigned so as to traverse for the next symbol.

Step  3: The statements are similar as that of decoding but the condition checks for each 1's and 0's in the string.

Step  4: For finding character of each code the empty string is joined.

Step  5: returns the string for the corresponding code.  

This simple code can be tested for the huffman code for a string or a sentence. The checking for decode can be found from the tree for which it has been used for encoding.For each character a unique code is assigned and that code can be used to decode to get the corresponding characters.



                                                   Thank you

Wednesday, 7 November 2012

Usb Accessory

Android supports a variety of USB peripherals and Android USB accessories through two modes: USB accessory and USB host. In USB accessory mode, the external USB hardware act as the USB hosts. The different accessories might include robotics controllers; docking stations; diagnostic and musical equipment; kiosks; card readers; and much more. This gives Android-powered devices that do not have host capabilities the ability to interact with USB hardware. Android USB accessories must be designed to work with Android-powered devices and must adhere to the Android accessory communication protocol. In USB host mode, the Android-powered device acts as the host. Examples of devices include digital cameras, keyboards, mice, and game controllers. USB devices that are designed for a wide range of applications and environments can still interact with Android applications that can correctly communicate with the device.
   Usb accessory is supported by devices with Android 2.3.4 or more versions. When an Android-powered device is in USB accessory mode, the attached Android USB accessory acts as the host, provides power to the USB bus, and enumerates connected devices.Although the USB accessory APIs were introduced to the platform in Android 3.1, they are also available in Android 2.3.4 using the Google APIs add-on library.


  • com.android.future.usb:  To support USB accessory mode in Android 2.3.4, the Google APIs add-on library includes the backported USB accessory APIs and they are contained in this namespace. This add-on library is a thin wrapper around the android.hardware.usb accessory APIs and does not support USB host mode. If you want to support the widest range of devices that support USB accessory mode, use the add-on library and import this package. It is important to note that not all Android 2.3.4 devices are required to support the USB accessory feature. Each individual device manufacturer decides whether or not to support this capability, which is why you must declare it in your manifest file.
  • android.hardware.usb: This namespace contains the classes that support USB accessory mode in Android 3.1. 

There are two usage differences between using the Google APIs add-on library and the platform APIs.
If you are using the add-on library, you must obtain the UsbManager object in the following manner:

 UsbManager manager = UsbManager.getInstance(this);
 
If you are not using the add-on library, you must obtain the UsbManager object in the following manner:

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
 
When you filter for a connected accessory with an intent filter, the UsbAccessory object is contained inside the intent that is passed to your application. If you are using the add-on library, you must obtain the UsbAccessory object in the following manner:

UsbAccessory accessory = UsbManager.getAccessory(intent);
 
If you are not using the add-on library, you must obtain the UsbAccessory object in the following manner:

UsbAccessory accessory = (UsbAccessory) intent.getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
 
The following codes that is to be changed in Manifest file in order accomplish usb accessory
  • Because not all Android-powered devices are guaranteed to support the USB accessory APIs, include a <uses-feature> element that declares that your application uses the android.hardware.usb.accessory feature.
  • If you are using the add-on library, add the <uses-library> element specifying com.android.future.usb.accessory for the library.
  • Set the minimum SDK of the application to API Level 10 if you are using the add-on library or 12 if you are using the android.hardware.usb package.
  • If you want your application to be notified of an attached USB accessory, specify an <intent-filter> and <meta-data> element pair for the android.hardware.usb.action.USB_ACCESSORY_ATTACHED intent in your main activity. The <meta-data> element points to an external XML resource file that declares identifying information about the accessory that you want to detect.

    In the XML resource file, declare <usb-accessory> elements for the accessories that you want to filter. Each <usb-accessory> can have the following attributes: 
  • manufacturer
  • model
  • version
Save the resource file in the res/xml/ directory. The resource file name (without the .xml extension) must be the same as the one you specified in the <meta-data> element.

Tuesday, 6 November 2012

Arduino ADK

ADK is a micro controller based on MEGA 2560. As a part of revision of micro-controller ADK is equipped with an USB Host interface.

Because the ADK is a USB Host, the phone will attempt to draw power from it when it needs to charge. When the ADK is powered over USB, 500mA total is available for the phone and board.The external power regulator can supply up to 1500mA. 750mA is available for the phone and ADK board. An additional 750mA is allocated for any actuators and sensors attached to the board. A power supply must be capable of providing 1.5A to use this much current.

 The Mega ADK board is a derivative of the Arduino Mega 2560. The modified Mega 2560 board includes a USB host chip. This host chip allows any USB device to connect to the Arduino. 

The USB host is not part of the original core of Arduino. To use the new features on this board you will need to include some libraries in your sketches.
There are three libraries needed to make the system work:
  • MAX3421e: handles the USB host chip
  • Usb: handles the USB communication
  • Android Accessory: checks if the device connecting is one of the available accessory-enabled phones

Android Accessories

An Android accessory is a physical accessory that can be attached to your Android device. These particular devices perform specific actions. With an Android phone and the Mega ADK, you can use whatever sensors and actuators you require to create your own accessories.
The USB accessory and the device check to make sure they are connected by passing back and forth product and vendor IDs. Google offers two accessory codes for people to try out: product IDs 0×2D00 and 0×2D01. Google has the USB vendor ID 0×1841. 


Arduino IDE 1.0

Arduino’s software is based on Processing’s IDE. The current release is version 0022. The Mega ADK has been developed as part of The 1.0 beta release. The libraries needed to make an Android accessory using the USB host chip are not included as part of version 0022, so you’ll need the 1.0 beta to work with the ADK and Android.
There are some fundamental changes in the way version 1.0 works, including a new extension for sketches. The suffix will change from *.pde to *.ino, any previous sketches will need to re-saved with the new extension.

Android SDK

The Android OS is based on Linux. Android Apps are made in a Java-like language running on a virtual machine called Dalvik.
Android offers a single download location to get the development software used by the different hardware manufacturers. This helps streamline development for different devices. You can get the Android SDK from the Android development website. You can easily upgrade to newer versions of the OS.
Google controls the main branch of the Android development system. They produce the core and the libraries that link the virtual machine with different peripherals.
If you’re going to make a commercially-sold accessory, it is your responsibility to:
  • port their drivers to each new version of the OS
  • create a ROM (functional image memory of a phone) that is compatible with that version of the OS
  • provide the developer’s community with a port of their drivers via the SDK upgrade system
The manufacturers are not always ready with ports the same time Google introduces a new revision of the OS. This has created an interesting parallel ROM development community dedicated to the creation of ROMs that include all the latest features yet capable of running on older devices. One of the most successful mods is Cyanogen.
For USB accessories to be supported on a particular device, there must be support for the accessory-mode, a special means of connecting over the USB port. This allows data transfer between devices and external peripherals.
Accessory mode is a feature of Android OS since version 2.3.4 Gingerbread and 3.1 Honeycomb.
Google suggests programming with the ADK using Eclipse and the Android SDK, together with Arduino’s IDE.
Eclipse is a multi-platform development environment. It performs operations like code prediction, error correction, project storage, and multiple workspace management.
To develop Android applications, the ADT (Android Development Tools) plugin is needed on top of Eclipse.

Artificial Neural Networks

It is a mathematical model or computational model that is inspired by the structure and functional aspects of biological neural networks. It consists of an interconnected group of artificial neurons and processes information using a connectionist approach for computation.
    The key element of this paradigm is the novel structure of the information processing system. It is composed of a large number of highly interconnected processing elements (neurons) working in union to solve specific problems. ANNs, like people, learn by example. An ANN is configured for a specific application, such as pattern recognition or data classification, through a learning process. Learning in biological systems involves adjustments to the synaptic connections that exist between the neurons.

  The motivation for artificial neural network(ANN) research is the belief that a human capabilities particularly in real time visual perception, speech understanding and sensory information processing and in adaptivity as well as intelligent decision making in general, come from organisational and computational principles exhibited in the highly complex neural network of the brain. Neural networks, with their remarkable ability to derive meaning from complicated or imprecise data, can be used to extract patterns and detect trends that are too complex to be noticed by either humans or other computer techniques.
   A trained neural network can be thought of as an "expert" in the category of information it has been given to analyse. This expert can then be used to provide projections given new situations of interest and answer "what if" questions.

Other advantages include:

Adaptive learning: An ability to learn how to do tasks based on the data given for training or initial experience.

Self-Organisation: An ANN can create its own organisation or representation of the information it receives during learning time.

Real Time Operation: ANN computations may be carried out in parallel, and special hardware devices are being designed and manufactured which take advantage of this capability.

Fault Tolerance via Redundant Information Coding:  Partial destruction of a network leads to the corresponding degradation.