Wcompress for 1D data
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
faten Abushmmala
il 23 Set 2022
Commentato: faten Abushmmala
il 27 Set 2022
is there another function in matlab for compressing 1D data (speech or any signal) that is equivalent to wcompress ?
4 Commenti
Sharmin Kibria
il 25 Set 2022
Did you check how if the threshold you chose zeroed out significant number of coefficients? If not, chances are the file size won't change drastically. The functions must be using different algorithms under the hood. Can you attach the audio file here? I want to take a look.
Risposta accettata
Walter Roberson
il 25 Set 2022
but i notice that the size of the data file after and before the wdencmp is the same,
What you are writing to the file is the reconstructed signal, not the wavelet coefficients. The size of the reconstructed signal is the same as the size of the input.
You are expecting that the output file will be smaller than the original file. That implies one of two possibilities:
- Perhaps you are getting confused about what is being written to the file, and are thinking that just the compressed coefficients are being written to the file; Or
- Perhaps you expecting that audiowrite() does compression on the signal it is asked to write, and you are expecting that the reconstructed signal will happen to be more compressible under whatever compression algorithm that audiowrite() might potentially be using.
But is audiowrite() compressing when you write with those parametes?
N = 32768;
Fs = 8000;
testsignal0 = zeros(N, 1); %should be entirely predictable
testsignal1 = linspace(-1, 1, N) .'; %should be pretty predictable
testsignal2 = rand(N, 1); %should be barely predictable
audiowrite('testsignal0.wav', testsignal0, Fs);
audiowrite('testsignal1.wav', testsignal1, Fs);
audiowrite('testsignal2.wav', testsignal2, Fs);
!ls -l testsignal*.wav
You can see that the files are all exactly the same size -- 2 bytes per sample (per channel) plus a 44 byte header.
So... any difference you are seeing in the size of the files is due to one of two possibilities:
- the original .wav file might have been written with a different audio encoder; Or
- the original .wav file had additional headers (such as copyright information or song name) that is not being written out when you audiowrite()
6 Commenti
Walter Roberson
il 27 Set 2022
If you use wavedec wavelet decomposition, then you get two outputs, there called c and l . c contains coefficients and l contains bookkeeping information about where the coefficients are.
If you then take a leading prefix of l and you extract the leading coefficients of c based upon the last value stored in the prefix of l, you can store those in a file. Then you can later retrieve them from the file and do wavrec() on them, and the signal will be reconstructed, just with a less-detailed signal.
if ispc()
filename = '5.wav';
[y, fs] = audioread(filename);
else
filename = which('handel.mat');
load(filename);
fs = Fs;
end
n = 3; w = 'db3';
Levels_to_drop = 1;
[c,l] = wavedec(y,n,w);
lprefix = l(1:end-Levels_to_drop);
cprefix_len = sum(lprefix(1:end-1));
cprefix = c(1:cprefix_len, :);
fsprefix = ceil(Fs * cprefix_len / size(y,1));
savefilename = 'test_compress.mat';
save(savefilename, 'cprefix', 'lprefix', 'fsprefix', '-v7');
clearvars -except filename savefilename w
load(savefilename)
yrec = waverec(cprefix, lprefix, w);
sound(yrec, fsprefix)
In the above, Levels_to_drop is the number of levels of decomposition coefficients to drop. With handel dropping one level results in something that is still quite good; dropping 2 levels results in something somewhat muddy.
Now comes the question: is the compressed file smaller?
dinfo = dir(filename);
orig_size = dinfo.bytes;
dinfo = dir(savefilename);
new_size = dinfo.bytes;
[~, orig_basename, ~] = fileparts(filename);
[~, new_basename, ~] = fileparts(savefilename);
fprintf('Original file "%s" was %d bytes, new file "%s" is %d bytes\n', orig_basename, orig_size, new_basename, new_size);
No! The new file is bigger -- notably so!
if ~ispc(); whos('-file', filename); end
whos('-file', savefilename)
lprefix and fsprefix are small; most of the space to store them in the .mat will be headers.
cprefix is only about half the number of elements (and so the number of bytes) and yet takes nearly twice as much space to store in the .mat .
Why? Well, MATLAB .mat -v7 files compress double variables by default, using a level 4 bzip algorithm (if I recall correctly) -- and the detail coefficients stored in c are considerably less compressable under that algorithm than the original sound !
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Filter Banks in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!