# CAN ANYONE PLEASE PROVIDE AN EXPLANATION FOR THIS CODE ON AUDIO COMPRESSION? (the formula is for 60% compression i dont understand how it came)

5 views (last 30 days)

Show older comments

[x,fs]=audioread('sample.wav');

N=length(x);

vlcplayer=audioplayer(x,fs);

vlcplayer.play

%%%%%%%%%%%%%%%%%%%%%

t=fft(x,N);

X=fftshift(t)

f=-fs/2:fs/N:(fs/2-fs/N);

figure(1)

plot(f,abs(X))

title('original audio signal')

%%%%%%%%%%%%%%%%%%%%%%%

Xr=zeros(1,N);

Xr((N*((60/100)/2))+1 : N*(1-(60/100)/2)) = X((N*((60/100)/2))+1 : N*(1-(60/100)/2)); %%FORMULA

##### 2 Comments

Rik
on 6 Nov 2020

### Answers (2)

KSSV
on 6 Nov 2020

[x,fs]=audioread('sample.wav'); % read the audio file

N=length(x); % Get the length if the data

vlcplayer=audioplayer(x,fs); % play the file

vlcplayer.play

%%%%%%%%%%%%%%%%%%%%%

t=fft(x,N); % this gives Fourier TRanform this converts the audio into frequency domain

X=fftshift(t) ; % fourier transform shifted

f=-fs/2:fs/N:(fs/2-fs/N); % gives frequencies based on length of the data

figure(1) % figure opened

plot(f,abs(X)) % plot the frequency and aboslute of X, plot of freuquency and amplitude

title('original audio signal') % title

%%%%%%%%%%%%%%%%%%%%%%%

Xr=zeros(1,N); % Initialization (You need to check on this)

Xr((N*((60/100)/2))+1 : N*(1-(60/100)/2)) = X((N*((60/100)/2))+1 : N*(1-(60/100)/2)); %%FORMULA

% The above formula generates some indices and picks that data from X and fills in Xr.

##### 5 Comments

KSSV
on 7 Nov 2020

fft calculates/ convers the time domain into frequency domain.

fftshift shifts the zero frequency to center.

Walter Roberson
on 7 Nov 2020

fft of real-valued data returns a vector in which the first half corresponds to increasing frequencies -- 0Hz, then 1Hz, then 2Hz, and so on up to (N-1)/2 Hz for N points. This corresponds to positive frequencies.

Then in the same vector it descends in frequency, (N-1)/2 Hz, (N-1)/2-1 Hz,... 3 Hz, 2 Hz, 1 Hz (0 Hz is not repeated). The information in the second half is reversed in order compared to the first half, and is the complex conjugate of the first half. This corresponds to negative frequencies.

Zero frequency, Positives, conj(reverse of Positives)

There is another order that contains the same information in a slightly different way:

conj(reverse of positives), zero frequency, Positives

This is just taking the second half of the fft and moving it to the front. When that is done, the data order corresponds to most negative frequency first, then increasing (less and less negative) until eventually you reach the zero frequency and then increase through the positive frequencies

For example regular order for fft might be

0 Hz, 1 Hz, 2 Hz, 3 Hz, -3 Hz, -2 Hz, -1 Hz

and the other order would be

-3 Hz, -2 Hz, -1 Hz, 0 Hz, 1 Hz, 2 Hz, 3 Hz

The function that does this minor reordering is fftshift()

As I indicated before, the effect is to make the extraction into a low-pass filter. Your code extracts the middle of the shifted data.. for example taking the middle it would extract the data for -1 Hz, 0 Hz, 1 Hz.

If you took the middle without doing the fftshift then what would have been extracted would be for 2 Hz, 3 Hz, -3 Hz which would be a high-pass filter

Walter Roberson
on 6 Nov 2020

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!