Audio compression and decompression
12 views (last 30 days)
Show older comments
For my code I generate a cosine wave using some notes to create a sound file. I need to compress my signal by removing a percentage of the samples (50%, 75%, 95%), but I have no idea how to do that using the fft function. Here is my code so far. Please help
ts=0.0001; %sampling rate
Fs=1/ts;
n=[0:1057]; %number of samples
%each note should be .625 long
t1=0:ts:0.625;
%frequency values of other notes and keyboard position%
bflat4=466.2;
d5=587;
f5=698.5;
bflat5=932;
ynote1=cos(2*pi*bflat4*t1);
ynote2=cos(2*pi*d5*t1);
ynote3=cos(2*pi*f5*t1);
ynote4=cos(2*pi*bflat5*t1);
ynote5=cos(2*pi*f5*t1);
ynote6=cos(2*pi*d5*t1);
ynote7=cos(2*pi*bflat4*t1);
ynote=[ynote1,ynote2,ynote3,ynote4,ynote5,ynote6,ynote7];
audiowrite('G:\test.wav',ynote,Fs)
0 Comments
Answers (2)
Lloyd Lobo
on 13 Nov 2020
The specific term you are looking for is downsampling. This will reduce the number of samples of a signal by simply deleting a sample once every n samples. If you remove too many samples, your sound may be altered slightly. You can use the matlab function:
1 Comment
Walter Roberson
on 13 Nov 2020
Suppose you are to compress by 74%. Then fft() and extract and retain (100-74) = 26% of the fft coefficients. You will have removed 74% of the information, so that will be compression.
To reconstruct the signal, know the original number of samples, and create a vector of zeros that long, and write the saved coefficients into that vector in the positions corresponding to where you extracted them from, leaving the ones you discarded as 0. Then you can ifft the result and play it.
Which 26% (or as appropriate) of the coefficients should you keep?
- keep the first entry in the fft() result. This entry is for frequency 0, and has to do with the mean of the signal
- for all entries beyond the first, retain them in pairs, one from the beginning and one from the end. If you keep entry (1+K) then also keep entry (end-K+1)
- In the special case of no compression, keep them all. This is equivalent to making a decision about what to do if the signal was originally an even length, so that after you keep the first entry, there are an odd number of remaining entries, so after pairing the entries from the beginning and the end, you are left with one in the middle. If you are doing any compression at all, that middle one should be zeroed.
Hint: fftshift() can make it easier to extract coefficients.
0 Comments
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!