Home > utils > scale_wvf.m

scale_wvf

PURPOSE ^

[scaled] = scale_wvf(wvf,dB,nbits,is_wav);

SYNOPSIS ^

function [scaled] = scale_wvf(wvf,dB,nbits,is_wav)

DESCRIPTION ^

 [scaled] = scale_wvf(wvf,dB,nbits,is_wav);
 
 Scales a waveform to a desired decibel level given a resolution of nbits.
 Default is 16 bits.
 Removes DC component
 If is_wav is set to true, the result is scaled on the range of -1 to 1 to be
 compatible with .WAV format [default=false]

 If wvf contains multiple columns, each column is scaled independently.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [scaled] = scale_wvf(wvf,dB,nbits,is_wav)
0002 % [scaled] = scale_wvf(wvf,dB,nbits,is_wav);
0003 %
0004 % Scales a waveform to a desired decibel level given a resolution of nbits.
0005 % Default is 16 bits.
0006 % Removes DC component
0007 % If is_wav is set to true, the result is scaled on the range of -1 to 1 to be
0008 % compatible with .WAV format [default=false]
0009 %
0010 % If wvf contains multiple columns, each column is scaled independently.
0011 
0012 % 01/05/05 PJ
0013 % 06/21/06 PJ Fixed a problem in that the scaling factor was being calculated
0014 %             based on the desired peak value and the rms of the waveform.
0015 
0016 % are we dealing with a situation where everything is scaled on a
0017 % range of -1 to 1
0018 try is_wav(1); catch is_wav = 0; end
0019 
0020 try nbits(1); catch nbits = 16; end
0021 
0022 max_dB = 20*log10(2^nbits);
0023 final_amp = 2^(nbits-1)/(10^((max_dB-dB)/20));
0024 
0025 % Remove any dc-component
0026 wvf = detrend(wvf,0);
0027 
0028 % If we are dealing with a waveform where the maximum absolute value is <1 then
0029 % scale to maximum representation before scaling to target dB value.
0030 if all(max(abs(wvf))) <= 1
0031   IS_WAV = 1;
0032   wvf = wvf * 2^(nbits-1);
0033 end
0034 
0035 % Calculate the RMS of the input waveform
0036 wvf_rms = sqrt(mean(wvf.^2));
0037 
0038 % Calculate an RMS scaling factor
0039 scale_factor = final_amp*sin(pi/4)./wvf_rms;
0040 
0041 % Apply the scaling factor
0042 scaled = wvf .* repmat(scale_factor,size(wvf,1),1);
0043 
0044 if is_wav
0045   scaled = scaled ./ (2^(nbits-1));
0046 end
0047 
0048 return
0049

Generated on Fri 22-Mar-2019 04:00:52 by m2html © 2003