adding resource code references to work from, playing

This commit is contained in:
aj 2020-10-30 19:54:44 +00:00
parent 7c30643c2c
commit 1828152664
10 changed files with 239 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*~
*#
*.pdf
samples
*.asv

View File

@ -0,0 +1,38 @@
close all;
clear all;
% PARAMS
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
% SIGNAL
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
% FINAL SIGNAL
%X = S + 2*randn(size(t));
X = S;
% PLOT TIME DOMAIN
figure(1)
plot(1000*t(1:50),X(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('t (milliseconds)')
ylabel('X(t)')
% CALCULATE FFT
Y = fft(X);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
% PLOT FREQ DOMAIN
figure(2)
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

View File

@ -0,0 +1,16 @@
close all;
clear all;
% b, a, x, (zi, dim)
% [y, zf]
% numerator = b
% demonitator = a
% signal = x
% initial conditions = zi
% dimension = dim
% final conditions = zf
% normalises a vector so a(1) is 1

View File

@ -0,0 +1,30 @@
close all;
clear all;
% GENERATE SIGNAl
noise = randn(50000,1);
x = filter(1,[1 1/2 1/3 1/4], noise); % 3rd order forward predictor
x = x(end-4096+1:end); % last 4096 samples of AR process to avoid startup transients
% LPC
a = lpc(x,3); % signal, filter order
est_x = filter([0 -a(2:end)],1,x);
% COMPARE LAST 100 SAMPLES OF EACH
plot(1:100, x(end-100+1:end), 1:100,est_x(end-100+1:end), '--')
grid
xlabel('Sample Number')
ylabel('Amplitude')
legend('Original signal','LPC estimate')
% PREDICTION ERROR
e = x - est_x;
[acs, lags] = xcorr(e,'coeff');
% PLOT AUTOCORRELATION
figure(2)
plot(lags, acs)
grid
xlabel('Lags')
ylabel('Normalized Autocorrelation')
ylim([-0.1 1.1])

18
lpss_autocorr.m Normal file
View File

@ -0,0 +1,18 @@
%% lpss_autocorr.m
%%
%% Load wav and run through autocorr from econometrics toolbox
%% Used max range of m lags calculated from sample frequency
close all;
clear all;
[y, Fs] = audioread('samples/hood_m.wav');
sample_size = size(y);
L = sample_size(1); % number of samples
f0 = 60; % low-pitched male speech
%f0 = 600; % children
m = Fs / f0; % from lecture 2, max number of lags required to search
autocorr(y,'NumLags', m)

32
lpss_fft.m Normal file
View File

@ -0,0 +1,32 @@
%% lpss_fft.m
%%
%% Load wav and plot in frequency domain using fft
%% Construct proper one-sided function for display
close all;
clear all;
[y, Fs] = audioread('samples/hood_m.wav');
sample_size = size(y);
L = sample_size(1); % number of samples
% PLOT TIME DOMAIN
figure(1);
title('time domain')
plot(y);
xlabel('t (milliseconds)');
ylabel('X(t)');
% CALCULATE FFT
Y = fft(y);
P2 = abs(Y/L); % two-sided spectrum
P1 = P2(1:L/2+1); % single-sided spectrum
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
% PLOT FFT
figure(2);
title('fft')
plot(f, P1);
xlabel('f (Hz)')
ylabel('|P1(f)|')

46
lpss_lpc.m Normal file
View File

@ -0,0 +1,46 @@
%% lpss_lpc.m
%%
%% Load wav and calculate LPC coeffs
close all;
clear all;
ORDER = 40;
DISPLAY_SAMPLES = 1000;
% READ SIGNAL
[y, Fs] = audioread('samples/hood_m.wav');
sample_size = size(y);
L = sample_size(1); % number of samples
DISPLAY_SAMPLES = min([DISPLAY_SAMPLES L]);
for ITER=1:ORDER
% LPC
a = lpc(y,ITER); % signal, filter order
est_y = filter(0.02, a, y);
% PREDICTION ERROR
e = y - est_y;
[acs, lags] = xcorr(e,'coeff');
% COMPARE TWO SIGNALS
x = 1:DISPLAY_SAMPLES;
figure(2)
plot(x, y(end-DISPLAY_SAMPLES+1:end), x, est_y(end-DISPLAY_SAMPLES+1:end), '--')
%plot(x, y(end-DISPLAY_SAMPLES+1:end))
%plot(x, est_y(end-DISPLAY_SAMPLES+1:end))
grid
xlabel('Sample Number')
ylabel('Amplitude')
legend('Original signal','LPC estimate')
% PLOT AUTOCORRELATION
% figure(2)
% plot(lags, acs)
% grid
% xlabel('Lags')
% ylabel('Normalized Autocorrelation')
% ylim([-0.1 1.1])
end

10
lpss_play.m Normal file
View File

@ -0,0 +1,10 @@
%% lpss_play.m
%%
%% Load wav and play
close all;
clear all;
[y, Fs] = audioread('samples/ee.wav');
size(y)
sound(y, Fs)

20
lpss_spectrogram.m Normal file
View File

@ -0,0 +1,20 @@
%% lpss_spectrogram.m
%%
%% Load wav and plot spectrogram
close all;
clear all;
WINDOW_NUMBER = 20;
% matrix by audio channel and sample frequency
[y, Fs] = audioread('samples/hood_m.wav');
sample_size = size(y);
window_size = round(sample_size(1) / ((WINDOW_NUMBER + 1)/2));
freqRes = Fs / window_size
timeRes = window_size / Fs
spectrogram(y, window_size, round(window_size/2), [], Fs, 'yaxis');
%view(-45, 65) % For spinning the map on its axis for a 3D view
%colormap bone % grayscale

27
lpss_spectrogram_iter.m Normal file
View File

@ -0,0 +1,27 @@
%% lpss_spectrogram_iter.m
%%
%% Load wav and plot spectrogram for iterating window sizes
close all;
clear all;
%WINDOW_NUMBER = 20;
% matrix by audio channel and sample frequency
[y, Fs] = audioread('samples/hood_m.wav');
sample_size = size(y);
for WINDOW_NUMBER=1:50
window_size = round(sample_size(1) / ((WINDOW_NUMBER + 1)/2));
freqRes = Fs / window_size;
timeRes = window_size / Fs;
spectrogram(y, window_size, round(window_size/2), [], Fs, 'yaxis');
%view(-45, 65) % For spinning the map on its axis for a 3D view
%colormap bone % grayscale
pause(0.05);
end