Question: code given % % Quantization _ SQNR - Histogram of Sounds % Evaluate the SQNR and sound volume distributions clear; close all; clc; isOctave =

code given %% Quantization_SQNR - Histogram of Sounds
% Evaluate the SQNR and sound volume distributions
clear; close all; clc;
isOctave = exist('OCTAVE_VERSION', 'builtin') ~=0; %1 if using Octave
if (isOctave) Fsize =14; else Fsize =12; end;
nbits =8; mu =255;
nbins =30;
%[y,fs]= audioread('RockGuitar.wav');
[y,fs]= audioread('/home/cgreco/Music/Bach/Bach_Prelude_in_E.wav');
y1_max = max(abs(y(:,1)));
y2_max = max(abs(y(:,2)));
% Scale signal
y(:,1)= y(:,1)/y1_max;
y(:,2)= y(:,2)/y2_max;
% Signal power
ndat = length(y(:,1));
py1= sum(y(:,1).^2)/ndat;
py1_dB = pow2db(py1);
py2= sum(y(:,2).^2)/ndat;
py2_dB = pow2db(py2);
%% Histograms for each channel
if isOctave
[n1,h1]= hist(y(:,1),nbins);
n1sum = sum(n1);
n1= n1/n1sum;
[n2,h2]= hist(y(:,2),nbins);
n2sum = sum(n2);
n2= n2/n2sum;
else
% h1= histogram(y(:,1),nbins);
% h2= histogram(y(:,2),nbins);
[n1,h1]= hist(y(:,1),nbins); % Not tested in Matlab
n1sum = sum(n1);
n1= n1/n1sum;
[n2,h2]= hist(y(:,2),nbins);
n2sum = sum(n2);
n2= n2/n2sum;
end
% Display Histograms
figure('position',[10,10,900,400])
subplot(1,2,1); bar(h1,n1,1);grid;
xlabel('Signal Magnitude');ylabel('Count Frequency');
title(['Channel 1, P_{avg}=',num2str(py1_dB),' dB']);
set(gca,'Fontsize',Fsize)
subplot(1,2,2);bar(h2,n2,1);grid;
xlabel('Signal Magnitude');ylabel('Count Frequency');
title(['Channel 2, P_{avg}=',num2str(py2_dB),' dB']);
set(gca,'Fontsize',Fsize)
% Quantization_SQNR
SQNR_uni =10*log10(3*py1)+6*nbits;
SQNR_comp =10*log10(3/(log(1+mu)^2))+6*nbits;
%% Instantaneous Power Histogram
ycomb =(y(:,1).^2+ y(:,2).^2)/2;
[n,hpow]= hist(ycomb,nbins);
nsum = sum(n);
n = n/nsum;
figure('position',[10,10,900,400])
subplot(1,2,1);bar((hpow),n,1);grid;
xlabel('Instantaneous Signal Power (W)');ylabel('Count Frequency');
title(['Average Instantaneous Power (Channels 1 and 2)']);
set(gca,'Fontsize',Fsize)
hpow_dB = pow2db(hpow);
subplot(1,2,2);bar(hpow_dB,n,10);grid;
xlabel('Instantaneous Signal Power (dB)');ylabel('Count Frequency');
title(['Average Instantaneous Power (Channels 1 and 2)']);
set(gca,'Fontsize',Fsize)
% SQNR
sqnr =6*nbits +10*log10(3*hpow);
figure()
bar(sqnr,n,10);grid;
xl = xlim;
xval =(xl(2)- xl(1))*0.45+ xl(1);
xlabel('SQNR (dB)');ylabel('Count Frequency');
title(['Instantaneous SQNR (uniform)']);
text(xval,0.8,['SQNR_{uniform}=',num2str(SQNR_uni)],'Fontsize',Fsize);
text(xval,0.7,['SQNR_{compressed}=',num2str(SQNR_comp)],'Fontsize',Fsize);
text(xval,0.6,['nbits =',int2str(nbits),',\mu =',int2str(mu)],'Fontsize',Fsize);
set(gca,'Fontsize',Fsize)
%% mu Law Compression
v=max(abs(y(:,1)));
xdmu=zeros(length(y(:,1)),2); % set up storage for decimated signal
xdmu(:,1)=compand(y(:,1),mu,v,'mu/compressor');
xdmu(:,2)=compand(y(:,1),mu,v,'mu/compressor');
[n3,h3]=hist(xdmu(:,1),nbins);
n3sum = sum(n3);
n3= n3/n3sum;
[n4,h4]=hist(xdmu(:,2),nbins);
n4sum = sum(n4);
n4= n4/n4sum;
figure('position',[10,10,900,400])
subplot(1,2,1);bar((h3),n3,1);grid;
xlabel('Compressed Signal Magnitude')
ylabel('Count Frequency')
title(['\mu Law Distribution. Channel 1(\mu =',int2str(mu),')'])
set(gca,'Fontsize',Fsize)
subplot(1,2,2);bar((h4),n4,1);grid;
xlabel('Compressed Signal Magnitude')
ylabel('Count Frequency')
title(['\mu Law Distribution, Channel 2(\mu =',int2str(mu),')'])
set(gca,'Fontsize',Fsize)
 code given %% Quantization_SQNR - Histogram of Sounds % Evaluate the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!