I want to add PSNR, MSError and SNR to my ready code.

Hello everyone, I'm doing research on an assignment that does LPC compression to a Speech file.
Here you see the code;
clear all;
clc;
%TAKING INPUT WAVEFILE,
a1 = 'C:\Users\user\Desktop\WAV\a1.wav';
[y, Fs] =audioread(a1);
% x=wavrecord(,);
%LENGTH (IN SEC) OF INPUT WAVEFILE,
t=length(y)./Fs;
sprintf('Processing the wavefile "%s"', a1)
sprintf('The wavefile is %3.2f seconds long', t)
%THE ALGORITHM STARTS HERE,
M=10; %prediction order
[aCoeff, pitch_plot, voiced, gain] = f_ENCODER(y, Fs, M); %pitch_plot is pitch periods
synth_speech = f_DECODER (aCoeff, pitch_plot, voiced, gain);
Additionally, I want this code to calculate PSNR, MSError and SNR.
dis=numel(y)-numel(A2);
A2=[A2,zeros(1,dis)];
PSNR = psnr(A2,y)
MSError=mse(A2,y)
SNR=snr(A2,y)
I found such a code on the internet. But I don't know what I should write instead of "A2" or for the others.

 Risposta accettata

I solved the problem, it works fine in this set of code;
s0=snr(y);
disp(" Original audio SNR in dB " + s0)
s1=snr(synth_speech);
disp(" LPC audio SNR in dB " + s1)

Più risposte (1)

Hassaan
Hassaan il 16 Gen 2024
Modificato: Hassaan il 16 Gen 2024
clear all;
clc;
% TAKING INPUT WAVEFILE
a1 = 'C:\Users\user\Desktop\WAV\a1.wav';
[y, Fs] = audioread(a1);
% LENGTH (IN SEC) OF INPUT WAVEFILE
t = length(y) / Fs;
disp(['Processing the wavefile "', a1, '"'])
disp(['The wavefile is ', sprintf('%3.2f', t), ' seconds long'])
% THE ALGORITHM STARTS HERE
M = 10; % Prediction order
[aCoeff, pitch_plot, voiced, gain] = f_ENCODER(y, Fs, M); % Encoder function
synth_speech = f_DECODER(aCoeff, pitch_plot, voiced, gain); % Decoder function
% Ensure the original and reconstructed signals are of the same length
dis = numel(y) - numel(synth_speech);
% Get the size of the synth_speech array
[synth_rows, synth_cols] = size(synth_speech);
% Ensure the zero-padding array has the same number of columns
zero_padding = zeros(dis, synth_cols);
% Pad the shorter signal
synth_speech_padded = [synth_speech; zero_padding];
% Calculate PSNR, MSE, and SNR
PSNR = psnr(synth_speech_padded, y);
MSError = immse(synth_speech_padded, y); % Mean Squared Error
SNR = snr(synth_speech_padded, y);
% Display the results
disp(['PSNR: ', num2str(PSNR), ' dB']);
disp(['Mean Squared Error: ', num2str(MSError)]);
disp(['SNR: ', num2str(SNR), ' dB']);
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

4 Commenti

I am getting error;
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in MAIN (line 16)
synth_speech_padded = [synth_speech; zeros(dis, 1)]; % Pad the shorter signal
Hassaan
Hassaan il 16 Gen 2024
Modificato: Hassaan il 16 Gen 2024
  • Made some adjustments regarding padding. Please re-check.
  • If still facing the issue. It would be helpful if you can share your specific 'f_ENCODER' and 'wav' file.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Feel free to contact me.
@Mert Sari please attach 'C:\Users\user\Desktop\WAV\a1.wav' so we can try it ourselves.
After the changes, I tried again and this time the error I experienced was;
Error using psnr>checkImages (line 97)
A and REF must have the same size.
Error in psnr (line 69)
checkImages(A,ref);
Error in MAIN (line 23)
PSNR = psnr(synth_speech_padded, y);
Here f_ENCODER code;
%ENCODER PORTION
%here, fs = sampling frequency
% aCoeff = LP coefficients
% pitch = pitch periods
% v = voiced or unvoiced decision bit
% g = gain of frames
function [aCoeff, pitch_plot, voiced, gain] = f_ENCODER(x, fs, M);
if (nargin<3), M = 10; end %prediction order=10;
%INITIALIZATION;
b=1; %index no. of starting data point of current frame
fsize = 30e-3; %frame size
frame_length = round(fs .* fsize); %=number data points in each framesize
%of "x"
N= frame_length - 1; %N+1 = frame length = number of data points in
%each framesize
%VOICED/UNVOICED and PITCH; [independent of frame segmentation]
[voiced, pitch_plot] = f_VOICED (x, fs, fsize);
%FRAME SEGMENTATION for aCoeff and GAIN;
for b=1 : frame_length : (length(x) - frame_length),
y1=x(b:b+N); %"b+N" denotes the end point of current frame.
%"y" denotes an array of the data points of the current
%frame
y = filter([1 -.9378], 1, y1); %pre-emphasis filtering
%aCoeff [LEVINSON-DURBIN METHOD];
[a, tcount_of_aCoeff, e] = func_lev_durb (y, M); %e=error signal from lev-durb proc
aCoeff(b: (b + tcount_of_aCoeff - 1)) = a; %aCoeff is array of "a" for whole "x"
%GAIN;
pitch_plot_b = pitch_plot(b); %pitch period
voiced_b = voiced(b);
gain(b) = f_GAIN (e, voiced_b, pitch_plot_b);
end
I uploaded a1.wav.

Accedi per commentare.

Prodotti

Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by