Compare commits
6 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
ed18cc2087 | |
|
|
4354d33e08 | |
|
|
5fd306c88f | |
|
|
4dabe5838c | |
|
|
4a81dac6b2 | |
|
|
b8ed5c4198 |
Binary file not shown.
|
|
@ -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
|
||||||
|
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
function [power, duration] = frequencySpectrum(signal, fs, pad)
|
||||||
|
%%%%%%%%%%%%%%%%%%
|
||||||
|
%function power = frequencySpectrum(signal, fs, pad)
|
||||||
|
%
|
||||||
|
% Task: Display the power spectrum (lin and log scale) of a given signal
|
||||||
|
%
|
||||||
|
% Input:
|
||||||
|
% - signal: the input signal to process
|
||||||
|
% - fs: the sampling rate
|
||||||
|
% -pad: boolean if true, signal is padded with 0 to the next power of 2 -> FFT instead of DFT
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% - power: the power spectrum
|
||||||
|
%
|
||||||
|
%
|
||||||
|
% Guillaume Gibert, guillaume.gibert@ecam.fr
|
||||||
|
% 25/04/2022
|
||||||
|
%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
|
n = length(signal); % number of samples
|
||||||
|
|
||||||
|
if (pad)
|
||||||
|
n = 2^nextpow2(n);
|
||||||
|
end
|
||||||
|
|
||||||
|
tic
|
||||||
|
y = fft(signal, n);% compute DFT of input signal
|
||||||
|
duration = toc;
|
||||||
|
|
||||||
|
power = abs(y).^2/n; % power of the DFT
|
||||||
|
|
||||||
|
[val, ind] = max(power); % find the mx value of DFT and its index
|
||||||
|
|
||||||
|
% plots
|
||||||
|
figure;
|
||||||
|
|
||||||
|
subplot(1,3,1) % time plot
|
||||||
|
t=0:1/fs:(n-1)/fs; % time range
|
||||||
|
%pad signal with zeros
|
||||||
|
if (pad)
|
||||||
|
signal = [ signal; zeros( n-length(signal), 1)];
|
||||||
|
end
|
||||||
|
plot(t, signal)
|
||||||
|
xticks(0:0.1*fs:n*fs);
|
||||||
|
xticklabels(0:0.1:n/fs);
|
||||||
|
xlabel('Time (s)');
|
||||||
|
ylabel('Amplitude (a.u.)');
|
||||||
|
|
||||||
|
subplot(1,3,2) % linear frequency plot
|
||||||
|
f = (0:n-1)*(fs/n); % frequency range
|
||||||
|
plot(f,power, 'b*'); hold on;
|
||||||
|
plot(f,power, 'r');
|
||||||
|
xlabel('Frequency (Hz)')
|
||||||
|
ylabel('Power (a.u.)')
|
||||||
|
|
||||||
|
subplot(1,3,3) % log frequency plot
|
||||||
|
plot(f,10*log10(power/power(ind)));
|
||||||
|
xlabel('Frequency (Hz)')
|
||||||
|
ylabel('Power (dB)')
|
||||||
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -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)');
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
pkg load signal;
|
||||||
|
|
||||||
|
[y, fs] = audioread("modulator22.wav");
|
||||||
|
range = 0:1/fs:(1/fs)*(size(y)-1);
|
||||||
|
#plot (range, y);
|
||||||
|
#xlabel ("Time (s)");
|
||||||
|
#ylabel ("Amplitude (arbitrary unit)");
|
||||||
|
b=fir1(30,1000/(fs/2));
|
||||||
|
yfiltered=filter(b,1,y);
|
||||||
|
|
||||||
|
a=butter(8,1000/(fs/2));
|
||||||
|
ybuttered=filter(a,1,y);
|
||||||
|
|
||||||
|
[h,w]=freqz(a,1)
|
||||||
|
freqz_plot(w,h);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
newfsdwn = 4000;
|
||||||
|
n=fs/newfsdwn;
|
||||||
|
ydwn = downsample(ybuttered, n);
|
||||||
|
audiowrite("modifiedwithdownsample.wav",ydwn,newfsdwn);
|
||||||
|
|
||||||
|
ydec = decimate(ybuttered,6);
|
||||||
|
fsndec = fs*size(ydec)/size(y);
|
||||||
|
audiowrite("modifiedwithdecimate.wav",ydec,fsndec);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
y_min = find(range >= 0,1);
|
||||||
|
y_max = find(range <= 0.3,1,"last");
|
||||||
|
yf= y(y_min:y_max);
|
||||||
|
|
||||||
|
[power, duration] = frequencySpectrum(ybuttered, fs, true);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
spectrogram(ybuttered, fs, 5, 30);
|
||||||
Binary file not shown.
|
|
@ -0,0 +1,32 @@
|
||||||
|
pkg load signal
|
||||||
|
|
||||||
|
modfile = 'C:/Users/Hercu/Documents/Git/signallab2/modulator22.wav';
|
||||||
|
carfile = 'C:/Users/Hercu/Documents/Git/signallab2/carrier22.wav';
|
||||||
|
outfile = "C:/Users/Hercu/Documents/Git/signallab2/vocodedsound.wav";
|
||||||
|
[modul, sr1] = audioread(modfile);
|
||||||
|
[carrier, sr2] = audioread(carfile);
|
||||||
|
[outfile, sr3] = audioread(outfile);
|
||||||
|
if sr1~=sr2, disp('your sampling rates dont match'); end
|
||||||
|
y = chanvocoder(carrier, modul, 512, 16, .2);
|
||||||
|
audiowrite("C:/Users/Hercu/Documents/Git/signallab2/outfile.wav",y,sr1)
|
||||||
|
tmod=0:1/sr1:length(modul)/sr1-1/sr1;
|
||||||
|
tcarrier=0:1/sr2:length(carrier)/sr2-1/sr2;
|
||||||
|
tout=0:1/sr3:length(outfile)/sr3-1/sr3;
|
||||||
|
|
||||||
|
subplot(3,1,1);
|
||||||
|
plot(tmod,modul);
|
||||||
|
xlabel("time(s)");
|
||||||
|
ylabel('amplitude modul (norm. unit)');
|
||||||
|
subplot(3,1,2);
|
||||||
|
plot(tcarrier,carrier);
|
||||||
|
xlabel("time(s)");
|
||||||
|
ylabel('amplitude carrier (norm. unit)');
|
||||||
|
subplot(3,1,3);
|
||||||
|
plot (tout,outfile);
|
||||||
|
xlabel("time(s)");
|
||||||
|
ylabel('amplitude outfile (norm. unit)');
|
||||||
|
|
||||||
|
|
||||||
|
spectrogram(modul, sr1, 10, 30);
|
||||||
|
spectrogram(carrier, sr2, 10, 30);
|
||||||
|
spectrogram(outfile, sr3, 10, 30);
|
||||||
Binary file not shown.
Loading…
Reference in New Issue