Audio compression using DCT

I'm working on Audio Compression using DCT. How do i expand the size of my sample after performing Inverse Discrete Cosine Transform? The code i have written produces an error saying "matrix dimensions must agree" when i try to find the MSE.
X=dct(y,32768);
s=framesize*X;
b=length(s');
ratio=N/b; % N is the length of the original sample
A=idct(X,32768);
[rows,columns]= size(A)
extra = rows*ceil(numel(X)/rows) - numel(X);
MU=reshape([X(:);nan(extra,1)],rows,columns)

5 Comments

hello
I do not get any error
where do you have an error ?
clc
clear
[y,Fs] =audioread('music.wav')
N=length(y);
[f0,idx] = pitch(y,Fs);
subplot(2,1,1)
plot(y)
ylabel('Amplitude')
xlabel('Sample Number')
x=[y];
n=round(length(x)/40);
p=zeros(n,40);
for k=0:39
p(:,k+1)=x(1+n*k:n*(k+1));
end
framesize=N/Fs;
M=round(y/framesize);
for...
i=1:1:M
if (i==M)
frame=y([(framesize*(i-1)+1):length(y)]);
else
frame=y([(framesize*(i-1)+1):framesize*i]);
end
end
X=dct(y,32768);
s=framesize*X;
b=length(s');
ratio=N/b;
A=idct(X,32768);
[rows,columns]= size(A)
extra = rows*ceil(numel(A)/rows) - numel(A);
MU=reshape([X(:);nan(extra,1)],rows,columns)
for...
i=1:1:M
if (i==M)
frame1=MU([(framesize*(i-1)+1):length(MU)]);
else
frame1=MU([(framesize*(i-1)+1):framesize*i]);
end
end
[R,C]= size(y);
err = (((y-MU).^2)/(R*C)); % error in this line
MSE=sqrt(err)
subplot(2,1,2)
plot(MU)
ylabel('Amplitude')
This is the code I wrote. Executing this gives me an error. Also, the size of MSE is very large.
hi
what is the error prompted at the command window ?
hum
I tried to help but I am blocked because I don't have the Aufio Toolbox
function pitch is notin my path
hope someone else will help you
Okay..Thank you.

Sign in to comment.

 Accepted Answer

M=round(y/framesize);
You have a problem there. You use M as a size in the code, but you calculate it based upon the content of y and y is a vector, so your M is a vector the same size as y.
for...
i=1:1:M
if (i==M)
frame=y([(framesize*(i-1)+1):length(y)]);
else
frame=y([(framesize*(i-1)+1):framesize*i]);
end
end
You overwrite all of frame in each iteration. If that is your intention, you might as well do only the last iteration, i=M
Likewise your later code overwrites all of frame1
X=dct(y,32768);
Your y is a vector so your X is a vector.
A=idct(X,32768);
Your X is a vector so your A is a vector.
[rows,columns]= size(A)
extra = rows*ceil(numel(A)/rows) - numel(A);
That is a waste of code. numel(A) is always going to be exactly divisible by rows unless rows is 0, so extra is going to always be 0.
MU=reshape([X(:);nan(extra,1)],rows,columns)
With extra being 0, that is just the same as reshape(X(:), rows, columns) but because of the way that X and A are built, X and A are already going to be the same shape.
err = (((y-MU).^2)/(R*C)); % error in this line
y is your original signal, of whatever size it is. You took a 32768 point dct to build X and Mu comes out the same as X, so MU is going to be a vector of length 32768. And it happens that both y and Mu are column vectors. But they are different lengths, y being the original size and MU being the 32768 length. You have a subtraction problem.
Perhaps you want
extra = numel(y) - numel(X);
MU = reshape([X(:), nan(extra,1)], size(y));
err = (((y-MU).^2)/numel(X); %?? numel(y) ??
but it seems pretty strange to deliberately do a lot of subtractions of nan. It would make more sense to subtract X from the first 32768 entries of y, or else to reconstruct from the dct coefficients back to the full size of y.

6 Comments

rachel cn
rachel cn on 16 Oct 2020
Edited: rachel cn on 16 Oct 2020
Thank you sir. Could you please tell me how do I reconstruct from the dct coefficients back to the full size of y and also how to stop frames from overwriting? I was actually trying to create frames and define them.
When you idct instead of passing in 32768, pass in the number of elements in y.
Thank you sir, it worked
Can audio compression be performed without creating frames?
Yes, certainly, you can compress the entire signal at one time.
However, audio tends to change over time, and wavelets are analyzing patterns that occur over the length of the entire section being analyzed. Imagine for a moment examining the opening notes of Beethoven's Fifth, and then a bit later on when the music has changed a fair bit. Trying to find a pattern that accounts for the entire symphony probably is not going to be as effective as breaking it up into segments so that you only need to worry about shorter-term patterns.
Thank you sir.

Sign in to comment.

More Answers (0)

Categories

Find more on Audio Processing Algorithm Design in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!