Contents

% quantization_error_audio2.m demo

% Copyright Anderson and Padgett 2009, 2011
% Last modified 7/5/09 WTP - original
% 6/17/11 WTP - adding dialog boxes for text output and flow control

clear all
close all
writefiles = 1;
loopcount = 0;
defaultbits = [3 4 6 8 10 12 14];



[speech,fs] = wavread('the pipe began to leak 2.wav');
while 1

new number of bits

    loopcount = loopcount + 1;
    if loopcount <= length(defaultbits)
        thisdefault = loopcount; % step through the defaultbits list until the end
    end
    qtxt = 'The speech file was recorded at 16 bit resolution. You may requantize it to any resolution you choose between 15 and 2 bits. You should at least try 4, 6, 8 and 10 bits. Are you ready to choose a number bits to quantize with? ';
    button = questdlg(qtxt,'Ready?','Choose Bits','Quit','Choose Bits');
    if strcmp(button,'Quit'), return; end;

    bits = 0;
    while bits<2 || bits>15
        prompt = 'How many bits would you like to use?';
        nbitstr = inputdlg(prompt,'Number of bits',1,cellstr(num2str(defaultbits(thisdefault))));
        if isempty(nbitstr)
            return;
        else
            [bits status] = str2num(nbitstr{1});
        end;
        if ~status, bits  = 0; end;
        if bits<2 || bits>15,  errordlg('Choose a value between 2 and 15','Bit Range Error','modal'); end;
        % this errdlg isn't acting like a modal dialog even though I tell
        % it to?
    end;

    % Assuming the 16 bit signal is stored in the wav file as integers
    % between 2^15 and -2^15, and then scaled back to a range of 1 to -1
    % (much as a Q1.15 number would be treated), we can re-quantize
    % by scaling to a new range and rounding to integers before scaling
    % back to the +/- 1 range.  The number of bits is reduced by one to
    % accomodate the natural range of 2^15 in a 16 bit signal.
    intspeech = round(2^(bits-1)*speech);
    qspeech = intspeech/2^(bits-1);

    if writefiles
        % write some wav files for those without Matlab
        qfilename = ['qspeech_' num2str(bits) 'bits.wav'];
        err = qspeech-speech;
        errfilename = ['norm_errspeech_' num2str(bits) 'bits.wav'];
        warning off % avoid clipping warnings due to unrepresentable values near 1
        wavwrite(qspeech,fs,qfilename)
        wavwrite(0.25*err/max(abs(err)),fs,errfilename)
        warning on
    end % writefiles

    range = 11300:11499;
    h1 = figure(1);
    op = get(h1,'OuterPosition');
    op(1) = 1;
    set(h1,'OuterPosition',op)

    subplot(311)
    plot(speech(range)); title(['Original Speech'])
    subplot(312)
    plot(qspeech(range)); title(['Quantized Speech - ' num2str(bits) ' bits'])
    subplot(313)
    plot(err(range)); title(['Quantization Error - ' num2str(bits) ' bits'])
    drawnow();

    h = msgbox('Playing Original','Info','replace');
    player = audioplayer(speech,fs,16);
    playblocking(player);
    close(h); % get rid of old message box

    h = msgbox('Playing Quantized','Info','replace');
    player = audioplayer(qspeech,fs,16);
    playblocking(player);
    close(h); % get rid of old message box

    h = msgbox('Playing Quantization Error (Normalized for audibility)','Info','replace');
    player = audioplayer(0.25*err/max(abs(err)),fs,16);
    playblocking(player);
    close(h); % get rid of old message box

    qtxt =  'Note that while the quantized speech remains (barely) intelligible at any bit resolution, the signal can be distinguished in the noise at both very high and very low bit resolutions.  This is because at low resolutions the quantization steps are large compared to the signal (violating our assumptions), and at high resolutions there are very few total error levels (preventing the error from being approximately uniform). If we were starting with an unquantized waveform the error would become more uniform as the number of bits increased.';
    button = questdlg(qtxt,'Info','OK','OK');
    disp(textwrap(cellstr(qtxt),80))
    'Note that while the quantized speech remains (barely) intelligible at any bit '
    'resolution, the signal can be distinguished in the noise at both very high and '
    'very low bit resolutions.  This is because at low resolutions the quantization '
    'steps are large compared to the signal (violating our assumptions), and at high '
    'resolutions there are very few total error levels (preventing the error from '
    'being approximately uniform). If we were starting with an unquantized waveform '
    'the error would become more uniform as the number of bits increased.'

    'Note that while the quantized speech remains (barely) intelligible at any bit '
    'resolution, the signal can be distinguished in the noise at both very high and '
    'very low bit resolutions.  This is because at low resolutions the quantization '
    'steps are large compared to the signal (violating our assumptions), and at high '
    'resolutions there are very few total error levels (preventing the error from '
    'being approximately uniform). If we were starting with an unquantized waveform '
    'the error would become more uniform as the number of bits increased.'

    'Note that while the quantized speech remains (barely) intelligible at any bit '
    'resolution, the signal can be distinguished in the noise at both very high and '
    'very low bit resolutions.  This is because at low resolutions the quantization '
    'steps are large compared to the signal (violating our assumptions), and at high '
    'resolutions there are very few total error levels (preventing the error from '
    'being approximately uniform). If we were starting with an unquantized waveform '
    'the error would become more uniform as the number of bits increased.'

    'Note that while the quantized speech remains (barely) intelligible at any bit '
    'resolution, the signal can be distinguished in the noise at both very high and '
    'very low bit resolutions.  This is because at low resolutions the quantization '
    'steps are large compared to the signal (violating our assumptions), and at high '
    'resolutions there are very few total error levels (preventing the error from '
    'being approximately uniform). If we were starting with an unquantized waveform '
    'the error would become more uniform as the number of bits increased.'

    'Note that while the quantized speech remains (barely) intelligible at any bit '
    'resolution, the signal can be distinguished in the noise at both very high and '
    'very low bit resolutions.  This is because at low resolutions the quantization '
    'steps are large compared to the signal (violating our assumptions), and at high '
    'resolutions there are very few total error levels (preventing the error from '
    'being approximately uniform). If we were starting with an unquantized waveform '
    'the error would become more uniform as the number of bits increased.'

    'Note that while the quantized speech remains (barely) intelligible at any bit '
    'resolution, the signal can be distinguished in the noise at both very high and '
    'very low bit resolutions.  This is because at low resolutions the quantization '
    'steps are large compared to the signal (violating our assumptions), and at high '
    'resolutions there are very few total error levels (preventing the error from '
    'being approximately uniform). If we were starting with an unquantized waveform '
    'the error would become more uniform as the number of bits increased.'

    'Note that while the quantized speech remains (barely) intelligible at any bit '
    'resolution, the signal can be distinguished in the noise at both very high and '
    'very low bit resolutions.  This is because at low resolutions the quantization '
    'steps are large compared to the signal (violating our assumptions), and at high '
    'resolutions there are very few total error levels (preventing the error from '
    'being approximately uniform). If we were starting with an unquantized waveform '
    'the error would become more uniform as the number of bits increased.'

end