Summary
Code of an Android app to calculate the maximum data rate of a channel according to Shannon and Nyquist.
Description
This tutorial shows the code used to create an Android application. The application calculates the maximum data rate of a channel. The two following formulas being used are:
Nyquist
maximum data rate = 2B log2 V
B is bandwidth
V is levels
This is represented by "MaximumDataRates.NyquistMdr(bandwidth, levels)"
Shannon
maximum number of bits/sec = B log2 (1 + S/N)
This is represented by "MaximumDataRates.ShannonMdr(decibels, bandwidth)"
B is bandwidth
S/N is signal-to-noise ratio
This a plain application. There are three parts to the application:
- Writing the MaximumDataRates.java class (static methods)
- Writing the MaximumDataRates_AndroidActivity.java (our main, and only, activity)
- Writing the main.xml file (res/layout/main.xml) for the presentation of the MaximumDataRates_AndroidActivity.java
Before we begin
For the tutorial, take a look at the following:
Shannon-Hartley theorem (wikipedia) - A more in-depth description.
What do you need to make it work? - When creating a new Android project in Eclipse, all you need to do is add the package at the top, make sure your activity name.java matches the name used inside the activity.java file, and copy everything in this main.xml to your main.xml.
Any questions? Please ask!
Maximum Data Rate (static methods)
public class MaximumDataRates {
// private no-arg constructor
// driving home the point
private MaximumDataRates(){
}
static double NyquistMdr(double bandwidth, double levels){
return(2 * bandwidth * (Math.log(levels) / Math.log(2)));
}
static double ShannonMdr(double decibels, double bandwidth){
return (bandwidth * Math.log((FindSignalToNoise(decibels) + 1)) / Math.log(2));
}
static double FindSignalToNoise(double decibels){
return Math.pow(10, (decibels / 10));
}
}
MaximumDataRates_AndroidActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
public class MaximumDataRates_AndroidActivity extends Activity {
/** Called when the activity is first created. */
// For the Nyquist TextBoxes
LinearLayout nyquistGroup;
EditText nyquistBandwidth;
EditText nyquistLevels;
TextView nyquistOutput;
// For the Shannon TextBoxes
LinearLayout shannonGroup;
EditText shannonBandwidth;
EditText shannonDecibels;
TextView shannonOutput;
// For the SNR TextBoxes
LinearLayout convertDecibelsToSnrGroup;
EditText snrDecibels;
TextView snrOutput;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Nyquist
nyquistGroup = (LinearLayout)findViewById(R.id.lnlNyquist);
nyquistBandwidth = (EditText)findViewById(R.id.txtNyquistBandwidth);
nyquistLevels = (EditText)findViewById(R.id.txtNyquistLevels);
nyquistOutput = (TextView)findViewById(R.id.lblNyquistOutput);
// Shannon
shannonGroup = (LinearLayout)findViewById(R.id.lnlShannon);
shannonBandwidth = (EditText)findViewById(R.id.txtShannonBandwidth);
shannonDecibels = (EditText)findViewById(R.id.txtShannonDecibels);
shannonOutput = (TextView)findViewById(R.id.lblShannonOutput);
// Convert Decibels to SNR
convertDecibelsToSnrGroup = (LinearLayout)findViewById(R.id.lnlConvertDecibelToSnr);
snrDecibels = (EditText)findViewById(R.id.txtSnrDecibels);
snrOutput = (TextView)findViewById(R.id.lblSnrOutput);
hideAllGroups();
nyquistGroup.setVisibility(0);
}
public void radioListener(View view)
{
RadioButton rb = (RadioButton) view;
if(rb.getText().toString().equals("Nyquist"))
{
hideAllGroups();
nyquistGroup.setVisibility(0);
nyquistOutput.setText("");
}
else if(rb.getText().toString().equals("Shannon"))
{
hideAllGroups();
shannonGroup.setVisibility(0);
shannonOutput.setText("");
}
else if(rb.getText().toString().equals("Decibels to SNR"))
{
hideAllGroups();
convertDecibelsToSnrGroup.setVisibility(0);
snrOutput.setText("");
}
}
public void calcNyquist(View view)
{
double bandwidth;
double levels;
String result = "";
try{
bandwidth = Double.parseDouble(nyquistBandwidth.getText().toString());
levels = Double.parseDouble(nyquistLevels.getText().toString());
result = Double.toString(MaximumDataRates.NyquistMdr(bandwidth, levels));
nyquistOutput.setText(result);
}
catch(Exception ex)
{
result = "Something is wrong with your input";
nyquistOutput.setText(result);
}
}
public void calcShannon(View view)
{
double bandwidth;
double decibels;
String result = "";
try{
bandwidth = Double.parseDouble(shannonBandwidth.getText().toString());
decibels = Double.parseDouble(shannonDecibels.getText().toString());
result = Double.toString(MaximumDataRates.ShannonMdr(decibels, bandwidth));
shannonOutput.setText(result);
}
catch(Exception ex){
result = "Something is wrong with your input";
shannonOutput.setText(result);
}
}
public void convertDecibelToSnr(View view)
{
double decibels;
String result = "";
try{
decibels = Double.parseDouble(snrDecibels.getText().toString());
result = Double.toString(MaximumDataRates.FindSignalToNoise(decibels));
snrOutput.setText(result);
}
catch(Exception ex)
{
result = "Something is wrong with your input";
snrOutput.setText(result);
}
}
public void hideAllGroups()
{
//outputGroup.setVisibility(0);
nyquistGroup.setVisibility(8);
shannonGroup.setVisibility(8);
convertDecibelsToSnrGroup.setVisibility(8);
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/radNyquist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioListener"
android:text="Nyquist" />
<RadioButton
android:id="@+id/radShannon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioListener"
android:text="Shannon" />
<RadioButton
android:id="@+id/radConvertDecibelToSnr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioListener"
android:text="Decibels to SNR" />
</RadioGroup>
<LinearLayout
android:id="@+id/lnlNyquist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/lblNyquistTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Nyquist: MDR=2B log2 V"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lblNyquistBandwidth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bandwidth" />
<EditText
android:id="@+id/txtNyquistBandwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/lblNyquistLevels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Levels" />
<EditText
android:id="@+id/txtNyquistLevels"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnNyquistCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calcNyquist"
android:text="Calculate" />
<TextView
android:id="@+id/label1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Result"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/lblNyquistOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="" />
</LinearLayout>
<LinearLayout
android:id="@+id/lnlShannon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/lblShannonTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shannon MDR=B log2 (1+SN)"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lblShannonBandwidth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bandwidth" />
<EditText
android:id="@+id/txtShannonBandwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lblShannonDecibels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Decibels" />
<EditText
android:id="@+id/txtShannonDecibels"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnShannonCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calcShannon"
android:text="Calculate" />
<TextView
android:id="@+id/label2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Result"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/lblShannonOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="" />
</LinearLayout>
<LinearLayout
android:id="@+id/lnlConvertDecibelToSnr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/lblConvertDecibelToSnrTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert Decibel to SNR"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/lblSnrDecibels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Decibels" />
<EditText
android:id="@+id/txtSnrDecibels"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSnrCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="convertDecibelToSnr"
android:text="Calculate" />
<TextView
android:id="@+id/label3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Result"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/lblSnrOutput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="" />
</LinearLayout>
</LinearLayout>
Amazing Post!
ReplyDeleteI love it. Will come back again - taking your feeds also.
Thanks.
iCaption That