Commit 35c671c5 authored by 袁通's avatar 袁通
Browse files

add Task2

parent 3789698b
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
function Freq_cochlea = cochlea(Freq_low, Freq_high, N)
    % divide the band
    d1 = log10((Freq_low/165.4)+1) / 0.06;
    d2 = log10((Freq_high/165.4)+1) / 0.06;
    d = linspace(d1, d2, N+1);
    Freq_cochlea = 165.4*(10**(0.06*d)-1);
end
 No newline at end of file
+34 −0
Original line number Diff line number Diff line
% Project 1, Task 2, Task 3
% load
[samples, Freq_sample] = audioread('C_01_01.wav');

% define
lpf = [20 50 100 400];
Freq_low = 200;
Freq_high = 7000;

% Set the number of bands N=4
N = 4;

figure();
hold on

for i in 1:length(lpf):
    yn = tone_vocoder(N, Freq_low, Freq_high, Freq_sample, lpf[i], sample)
    yn = fftshift(fft(yn));
    yn_abs = abs(yn);

    %plot yn_abs
    subplot(4, 1, i);
    xn = linspace(-Freq_sample/2, Freq_sample/2, length(sample))
    plot(xn, yn_abs);
    title(sprintf('N = 4, f_{lpf}=%d Hz, Tone Vocoder',lpf(i)))

    xlabel('Frequency(Hz)');ylabel('Magnitude');
    audiowrite(sprintf('task2_N=4_lpf=%d.wav',lpf(n)),yn,Freq_sample);
    saveas(figure1,sprintf('task2_lpf%d.png',lpf(i)));

    grid on

end
+33 −0
Original line number Diff line number Diff line
function yn = tone_vocoder(N, Freq_low, Freq_high, Freq_sample, Freq_filter, sample)
    % divide the frequency
    Freq_cochlea = cochlea(Freq_low, Freq_high, N);
    yn = zeros(length(sample))

    for band_index = 1:N
        band = [Freq_cochlea[band_index] Freq_cochlea[band_index+1]]

        % Design band-pass filter at band i
        band_pass = butter(4, band/(Freq_sample/2));
        
        % Do band-pass filtering at band i
        sample_band = filter(band_pass, sample);

        % Do full-wave rectification
        % apply low-pass filtering to get the envelope at band i
        low_pass = butter(2, Freq_filter/(Freq_sample/2), 'low');
        filtered_sample_band = filter(low_pass, sample_band)

        % Generate a sinewave, whose frequency equals to the center frequency of the i-th bandpass filter
        fm = (band[1]+band[2])/2
        xn = 1:length(sample)
        sin_wave = sin(2*pi*fm*xn/Freq_sample)

        % Multiply the envelope signal and sinewave
        yn = yn + sin_wave .* filtered_sample_band
    end

    yn = yn/norm(yn)*norm(sample);
    yn = yn.';
    

end
 No newline at end of file