added chanvocoder and spectrogram

This commit is contained in:
Gabri6 2023-03-24 10:53:29 +01:00
parent 1603e93d51
commit 21202dfa75
3 changed files with 100 additions and 2 deletions

57
chanvocoder.m Normal file
View File

@ -0,0 +1,57 @@
function y = chanvocoder(carrier, modul, chan, numband, overlap)
% y = chanvocoder(carrier, modul, chan, numband, overlap)
% The Channel Vocoder modulates the carrier signal with the modulation signal
% chan = number of channels (e.g., 512)
% numband = number of bands (<chan) (e.g., 32)
% overlap = window overlap (e.g., 1/4)
if numband>chan
error('# bands must be < # channels')
end
[rc, cc] = size(carrier);
if cc>rc
carrier = carrier';
end
[rm, cm] = size(modul);
if cm>rm
modul = modul';
end
st = min(rc,cc); % stereo or mono?
if st~= min(rm,cm)
error('carrier and modulator must have same number of tracks');
end
len = min(length(carrier),length(modul)); % find shortest length
carrier = carrier(1:len,1:st); % shorten carrier if needed
modul = modul(1:len,1:st); % shorten modulator if needed
L = 2*chan; % window length/FFT length
w = hanning(L);
if st==2
w=[w w];
end % window/ stereo window
bands = 1:round(chan/numband):chan; % indices for frequency bands
bands(end) = chan;
y = zeros(len,st); % output vector
ii = 0;
while ii*L*overlap+L <= len
ind = round([1+ii*L*overlap:ii*L*overlap+L]);
FFTmod = fft( modul(ind,:) .* w ); % window & take FFT of modulator
FFTcar = fft( carrier(ind,:) .* w ); % window & take FFT of carrier
syn = zeros(chan,st); % place for synthesized output
for jj = 1:numband-1 % for each frequency band
b = [bands(jj):bands(jj+1)-1]; % current band
syn(b,:) = FFTcar(b,:)*diag(mean(abs(FFTmod(b,:))));
end % take product of spectra
midval = FFTmod(1+L/2,:).*FFTcar(1+L/2,:); % midpoint is special
synfull = [syn; midval; flipud( conj( syn(2:end,:) ) );]; % + and - frequencies
timsig = real( ifft(synfull) ); % invert back to time
y(ind,:) = y(ind,:) + timsig; % add back into time waveform
ii = ii+1;
end
y = 0.8*y/max(max(abs(y))); % normalize output

38
spectrogram.m Normal file
View File

@ -0,0 +1,38 @@
function spectrogram(signal, samplingFreq, step_size, window_size)
%%%%%%%%%%%%%%%%%%%%%%%
%function spectrogram(signal, samplingFreq, step_size, window_size)
% ex.: spectrogram(signal, samplingFreq, step_size, window_size)
%
% Task: Plot the spectrogram of a given signal
%
% Inputs:
% -signal: temporal signal to analyse
% -samplingFreq: sampling frequency of the temporal signal
% -step_size: how often the power spectrum will be computed in ms
% -window_size: size of the analysing window in ms
%
% Ouput: None
%
% author: Guillaume Gibert (guillaume.gibert@ecam.fr)
% date: 14/03/2023
%%%%%%%%%%%%%%%%%%%%%%%
figure;
subplot(2,1,1);
t=0:1/samplingFreq:length(signal)/samplingFreq-1/samplingFreq;
plot(t, signal');
xlim([0 length(signal)/samplingFreq-1/samplingFreq]);
ylabel('amplitude (norm. unit)');
subplot(2,1,2);
step = fix(step_size*samplingFreq/1000); % one spectral slice every step_size ms
window = fix(window_size*samplingFreq/1000); % window_size ms data window
fftn = 2^nextpow2(window); % next highest power of 2
[S, f, t] = specgram(signal, fftn, samplingFreq, window, window-step);
S = abs(S(2:fftn*4000/samplingFreq,:)); % magnitude in range 0<f<=4000 Hz.
S = S/max(S(:)); % normalize magnitude so that max is 0 dB.
S = max(S, 10^(-40/10)); % clip below -40 dB.
S = min(S, 10^(-3/10)); % clip above -3 dB.
imagesc (t, f, log(S)); % display in log scale
set (gca, "ydir", "normal"); % put the 'y' direction in the correct direction
xlabel('time (s)');
ylabel('frequency (Hz)');

View File

@ -22,10 +22,13 @@ plot(t,y)
%ylabel('Sound (dB)');
%Frequency spectrum using DFT
[powerDFT durationDFT] = frequencySpectrum(y, fs, false);
%%%%%[powerDFT durationDFT] = frequencySpectrum(y, fs, false);
%Frequency spectrum using FFT
[powerFFT durationFFT] = frequencySpectrum(y, fs, true);
%%%%%[powerFFT durationFFT] = frequencySpectrum(y, fs, true);
sprintf(["time used for DFT: " num2str(durationDFT)])
sprintf(["time used for FFT: " num2str(durationFFT)])
spectrogram(y, fs, 5, 30);
spectrogram(y, fs, 5, 5);