load handel whos % y sound signal vector % Fs sample frequency x = [0:length(y)-1]; x = x/Fs; %Fs is 8192 % so if Fs is 8192 what is the step size of x? 1/Fs, so x is incremented by .000122 at each step x(1:10) plot(x,y); xlabel('time in seconds'); ylabel('amplitude of signal (displacement)'); sound(y) % can easily pull out pieces of the signal by indexing into the vector plot(x(1:4000),y(1:4000)); xlabel('time in seconds'); ylabel('amplitude of signal (displacement)'); sound(y(1:4000)); % let's take a look at quantization ll = 1601; ul=2200; clf plot(x(ll:ul),y(ll:ul)); xlabel('time in seconds'); ylabel('amplitude of signal (displacement)'); sound(y(ll:ul)); % uniform quantization hold on plot(repmat([x(ll) x(ul)]',[1,17]),repmat([-1:(1/2^3):1],[2 1]),'g-') % quantization levels [-1:(1/2^3):1] % quantize using 4 bits - 2^4 levels = 16 levels of quantization y4bit = round(y*(1+1/2^3)/(1/2^3)); y4bit = y4bit*(1/2^3); % zoom in on a section plot(x(ll:ul),y4bit(ll:ul),'r-'); % average quantization error mean(abs(y4bit-y)) % sound at 4 bits of quantization sound(y4bit); % now let's try 8 bits of quantization y8bit = round(y*(1+1/2^7)/(1/2^7)); y8bit = y8bit*(1/2^7); % zoom in on a section - quantization error has gotten much smaller clf plot(x(ll:ul),y(ll:ul)); xlabel('time in seconds'); ylabel('amplitude of signal (displacement)'); hold on plot(x(ll:ul),y8bit(ll:ul),'r-'); % average quantization error mean(abs(y8bit-y)) sound(y8bit); % now 16 bit y16bit = round(y*(1+1/2^15)/(1/2^15)); y16bit = y16bit*(1/2^15); sound(y16bit); clf hist(y,50); % let's look at quantization % remember uniform quantization looks like this ll = 1601; ul=2200; clf plot(x(ll:ul),y(ll:ul)); xlabel('time in seconds'); ylabel('amplitude of signal (displacement)'); % uniform quantization hold on plot(repmat([x(ll) x(ul)]',[1,17]),repmat([-1:(1/2^3):1],[2 1]),'g-') % non-uniform quantization might look something like this clf plot(x(ll:ul),y(ll:ul)); xlabel('time in seconds'); ylabel('amplitude of signal (displacement)'); % non-uniform quantization -- quantize at a finer level near 0 and coarser near +/-1 ys = sort(y); breaks = ys([round(1:length(ys)/16:length(ys)) length(ys)]); hold on plot(repmat([x(ll) x(ul)]',[1,17]),repmat(breaks(:)',[2 1]),'g-') % quantization error visualization d = abs(repmat(y,[1,length(breaks)])-repmat(breaks(:)',[size(y,1),1])); [ min_d, min_d_pos ]=min(d,[],2); y4bit_non_linear = breaks(min_d_pos); % here we see that at the places where you quantize at a finer level, error is smaller % while at the coarser level errors are larger (but at high amplitudes you expect not % to be able to here these errors as much as at low amplitudes - hence the use of % non-uniform quantization). plot(x(ll:ul),y4bit_non_linear(ll:ul),'r-'); % zoom in on a section % sound with 4 uniform bits of quantization sound(y4bit(1:24000)) % is more noisy than sound with 4 non-uniform bits of quantization sound(y4bit_non_linear(1:24000)) % and quantization error is also smaller for non-linear than linear % uniform quantization error mean(abs(y4bit-y)) % non-uniform quantization error mean(abs(y4bit_non_linear-y))