From 1828152664debc53ecf6c3ab4629817631475210 Mon Sep 17 00:00:00 2001 From: aj Date: Fri, 30 Oct 2020 19:54:44 +0000 Subject: [PATCH] adding resource code references to work from, playing --- .gitignore | 2 ++ examples scripts/lpss_fft_example.m | 38 +++++++++++++++++++++ examples scripts/lpss_filter_example.m | 16 +++++++++ examples scripts/lpss_lpc_example.m | 30 +++++++++++++++++ lpss_autocorr.m | 18 ++++++++++ lpss_fft.m | 32 ++++++++++++++++++ lpss_lpc.m | 46 ++++++++++++++++++++++++++ lpss_play.m | 10 ++++++ lpss_spectrogram.m | 20 +++++++++++ lpss_spectrogram_iter.m | 27 +++++++++++++++ 10 files changed, 239 insertions(+) create mode 100644 examples scripts/lpss_fft_example.m create mode 100644 examples scripts/lpss_filter_example.m create mode 100644 examples scripts/lpss_lpc_example.m create mode 100644 lpss_autocorr.m create mode 100644 lpss_fft.m create mode 100644 lpss_lpc.m create mode 100644 lpss_play.m create mode 100644 lpss_spectrogram.m create mode 100644 lpss_spectrogram_iter.m diff --git a/.gitignore b/.gitignore index a27c9ea..f73396e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *~ *# *.pdf +samples +*.asv diff --git a/examples scripts/lpss_fft_example.m b/examples scripts/lpss_fft_example.m new file mode 100644 index 0000000..6669a41 --- /dev/null +++ b/examples scripts/lpss_fft_example.m @@ -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)|') \ No newline at end of file diff --git a/examples scripts/lpss_filter_example.m b/examples scripts/lpss_filter_example.m new file mode 100644 index 0000000..268b5fe --- /dev/null +++ b/examples scripts/lpss_filter_example.m @@ -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 \ No newline at end of file diff --git a/examples scripts/lpss_lpc_example.m b/examples scripts/lpss_lpc_example.m new file mode 100644 index 0000000..7630ad3 --- /dev/null +++ b/examples scripts/lpss_lpc_example.m @@ -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]) \ No newline at end of file diff --git a/lpss_autocorr.m b/lpss_autocorr.m new file mode 100644 index 0000000..0a5b564 --- /dev/null +++ b/lpss_autocorr.m @@ -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) \ No newline at end of file diff --git a/lpss_fft.m b/lpss_fft.m new file mode 100644 index 0000000..385e38e --- /dev/null +++ b/lpss_fft.m @@ -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)|') \ No newline at end of file diff --git a/lpss_lpc.m b/lpss_lpc.m new file mode 100644 index 0000000..37c6346 --- /dev/null +++ b/lpss_lpc.m @@ -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 \ No newline at end of file diff --git a/lpss_play.m b/lpss_play.m new file mode 100644 index 0000000..6eeb431 --- /dev/null +++ b/lpss_play.m @@ -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) \ No newline at end of file diff --git a/lpss_spectrogram.m b/lpss_spectrogram.m new file mode 100644 index 0000000..ef0171f --- /dev/null +++ b/lpss_spectrogram.m @@ -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 \ No newline at end of file diff --git a/lpss_spectrogram_iter.m b/lpss_spectrogram_iter.m new file mode 100644 index 0000000..219fa1f --- /dev/null +++ b/lpss_spectrogram_iter.m @@ -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 \ No newline at end of file