Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ValDB = db(X, Y, Z )
- % Convert a value to decibel.
- % Arguments:
- %
- % X ... the ratio to be converted to decibel
- % Y ... second argument, either a mode string ('Power', 'power', 'Voltage', 'voltage') or a numeric resistor value
- % Z ... third argument, numeric resistor value
- % Constant strings that select the mode of operation
- VOLTAGE_NAMES = {'Voltage', 'voltage'};
- POWER_NAMES = {'Power', 'power'};
- USAGE = 'Placeholder for a very verbose desciption.';
- if(nargin == 1)
- U='voltage';
- R = 1;
- elseif(nargin == 2)
- if(ischar(Y))
- U = Y;
- R = 1;
- elseif(isnumeric(Y))
- U = 'voltage';
- R = Y;
- else
- disp USAGE
- return
- end
- elseif(nargin == 3)
- if( ischar(Y) && isnumeric(Z) )
- U = Y;
- R = Z;
- else
- disp USAGE
- return
- end
- else
- disp USAGE
- return
- end
- switch U
- case VOLTAGE_NAMES
- Factor = 20;
- case POWER_NAMES
- Factor = 10;
- R = 1; % Ignore any value for R
- otherwise
- disp( USAGE );
- return
- end
- % Convert ratio to decibel
- ValDB = Factor * log10( X ) - 10 * log10(R);
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement