/** * title: Bandwidth Calculator * author: Matt Rankin, 2007 */ public class Bandwidth{ static char type; static double hertz,depth,signal,noise,dB,bitrate; public static void main(String[] argv){ if(argv.length<3 || argv.length>3) type='h'; else type=(argv[0].matches("-[nsh]"))?argv[0].charAt(1):'h'; switch(type){ case 'n': try{ hertz=Double.parseDouble(argv[1]); depth=Double.parseDouble(argv[2]); }catch(NumberFormatException e){ epicFail(); } bitrate=nyquist(hertz,depth); System.out.println("bitrate = "+bitrate+"bps"); break; case 's': hertz=Double.parseDouble(argv[1]); if(argv[2].matches("\\d+:\\d+")){ String[] ratio=argv[2].split(":"); signal=Double.parseDouble(ratio[0]); noise=Double.parseDouble(ratio[1]); }else{ dB=Double.parseDouble(argv[2]); signal=Math.pow(10,dB/10); noise=1; } bitrate=shannon(hertz,signal,noise); System.out.println("bitrate = "+bitrate+"bps"); break; case 'h': System.out.println(help()); break; } System.exit(0); } static double nyquist(double h,double d){ return 2*h*logb(d,2); } static double shannon(double h,double s,double n){ double e=Math.pow(10,4); return Math.round(e*h*logb((1+(s/n)),2))/e; } static double logb(double d,double b){ return Math.log(d)/Math.log(b); } static String help(){ String h; h="Calculates channel bandwidth capacity using either Nyquist's or "+ "Shannon's\ntheorem.\n\n"+ " Bandwidth -[nsh] hertz [depth | [dB | SNR]]\n\n"+ " -n Use Nyquist's Theorem\n"+ " -s Use Shannon's Theorem\n"+ " -h Help\n"+ " hertz Channel bandwidth in cyles per second\n"+ " depth No. of signalling symbols (2 for binary)\n"+ " dB Signal strength in decibels\n"+ " SNR Signal-to-Noise ratio (syntax: 's:n')\n\n"+ "Nyquist's theorem requires the 'hertz' and 'depth' parameters to\n"+ "calculate the maximum bitrate of a noise-free bandwidth-limited\n"+ "channel. Shannon's theorem applies to a bandwidth-limited noisy\n"+ "channel without respect to the number of signal cycles used, and uses\n"+ "the 'hertz' and 'dB' or 'SNR' parameters."; return h; } static void epicFail(){ System.err.println("Epic fail!"); System.exit(0); } }