Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2011  |  syntax: MatLab  |  size: 1.04 KB  |  hits: 127  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function ValDB = db(X, Y, Z )
  2. % Convert a value to decibel.
  3. % Arguments:
  4. %
  5. % X ... the ratio to be converted to decibel
  6. % Y ... second argument, either a mode string ('Power', 'power', 'Voltage', 'voltage') or a numeric resistor value
  7. % Z ... third argument, numeric resistor value
  8.  
  9. % Constant strings that select the mode of operation
  10. VOLTAGE_NAMES   = {'Voltage', 'voltage'};
  11. POWER_NAMES     = {'Power', 'power'};
  12. USAGE           = 'Placeholder for a very verbose desciption.';
  13.  
  14. if(nargin == 1)
  15.         U='voltage';
  16.         R = 1;
  17. elseif(nargin == 2)
  18.  
  19.         if(ischar(Y))
  20.                 U = Y;         
  21.                 R = 1;
  22.                
  23.         elseif(isnumeric(Y))
  24.                 U = 'voltage';
  25.                 R = Y;
  26.         else
  27.                 disp USAGE
  28.                 return
  29.         end
  30. elseif(nargin == 3)
  31.  
  32.         if( ischar(Y) && isnumeric(Z) )
  33.                 U = Y;
  34.                 R = Z;
  35.         else
  36.                 disp USAGE
  37.                 return
  38.         end
  39. else
  40.         disp USAGE
  41.         return
  42. end
  43.  
  44. switch U
  45.         case VOLTAGE_NAMES
  46.                 Factor = 20;
  47.         case POWER_NAMES
  48.                 Factor = 10;
  49.                 R = 1;          % Ignore any value for R
  50.         otherwise
  51.                 disp( USAGE );
  52.                 return
  53. end
  54.  
  55. % Convert ratio to decibel
  56. ValDB = Factor * log10( X ) - 10 * log10(R);
  57.  
  58. end