-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.m
79 lines (49 loc) · 1.77 KB
/
encoding.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
clear all
clear variables
% A basic codec
musicFile = dir('musique.wav');
[y,Fs] = audioread('musique.wav');
bitsLength = musicFile.bytes * 8;
fprintf("Sampling frequency : %f\n", Fs);
fprintf("Length in bits : %f\n", bitsLength);
sound(y, Fs);
musicEncodedFile = "musicEncoded.wav";
[npt, scale] = coder(y, Fs, bitsLength, 2000, 2500, musicEncodedFile);
fprintf("Scale : %f\n", scale);
fprintf("Number of Points : %f\n", npt);
function [npt, scale] = coder(y, fs, bits, fmin, fmax, file)
% tfd computation, then ensures column vector
tfy = fft(y);
tfy = tfy(:);
% From frequency mask to matlab index
npt = length(y);
kmin = round(npt * fmin / fs) + 1;
kmax = round(npt * fmax / fs) + 1;
% Mask
tfymasq = tfy(kmin:kmax);
% Real and imaginary part are stored
% as both channels of a stereo signal
tfymasq = [real(tfymasq) imag(tfymasq)];
% Ensures maximum value lower than 1
scale = max(max(abs(tfymasq))) * 1.01;
tfymasq = tfymasq / scale;
% Stores with same quantization
audiowrite(file, tfymasq, fs)
%, bits)
end
function [y, fs, bits] = decoder(file, fmin, fmax, npt, scale)
% reads the windowed Fourier transform stored in file fichier (wavread);
[tfy, Fs] = audioread('musicEncoded.wav');
% rebuild the original Fourier transform with the assumption that the missing values are null, and rescale it
% properly;
tfy = tfy * scale;
tfy = tfy(:);
% computes the inverse Fourier transform;
y = ifft(tfy);
%returns the decoded signal y, and the same output arguments than function wavread.
end
function [f, tfx] = transffourier(y, Nf, fs)
k = 0:Nf-1;
f = k*fs/Nf;
tfx = 1/fs*fft(y, Nf);
end