From 1703a3e368ab358e6b31371d06f4ff753f8e786d Mon Sep 17 00:00:00 2001 From: Sam Perry Date: Wed, 12 Oct 2016 09:47:35 +0100 Subject: [PATCH] Continued with exercises --- main.m | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ movingAverage.m | 7 ++++--- plotPeak.m | 16 ---------------- plotPeakWave.m | 26 ++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 main.m delete mode 100644 plotPeak.m create mode 100644 plotPeakWave.m diff --git a/main.m b/main.m new file mode 100644 index 0000000..f82b44c --- /dev/null +++ b/main.m @@ -0,0 +1,49 @@ +% Main function for running exercises +function main() + % Parse data from text file into matlab object. + bouydata = readbuoydata('045200603.txt'); + + % Plot peak period and wave height to graphs + %plotPeakWave(bouydata.Tp, bouydata.Hs, bouydata.date) + + % Apply moving average filter to + plotPeakMovingAverage(bouydata); + +function plotPeakMovingAverage(bouydata) + % Unpack object into variables. + peak_period = bouydata.Tp; + timestamps = bouydata.date; + + % Apply moving average filter with M = 5, 21 and 51 to peak period data. + ma5 = movingAverage(peak_period, 5); + ma21 = movingAverage(peak_period, 21); + ma51 = movingAverage(peak_period, 51); + + figure + subplot(3,1,1) + plot(timestamps, peak_period, 'color', [0.8 0.8 0.8]) + hold on; + plot(timestamps, ma5); + title('M = 5') + xlabel('Time') + ylabel('Peak Period') + datetick('x', 0) + + + subplot(3,1,2) + plot(timestamps, peak_period, 'color', [0.8 0.8 0.8]) + hold on; + plot(timestamps, ma21); + title('M = 21') + xlabel('Time') + ylabel('Peak Period') + datetick('x', 0) + + subplot(3,1,3) + plot(timestamps, peak_period, 'color', [0.8 0.8 0.8]) + hold on; + plot(timestamps, ma51); + title('M = 51') + xlabel('Time') + ylabel('Peak Period') + datetick('x', 0) diff --git a/movingAverage.m b/movingAverage.m index 5c30d4b..314227c 100644 --- a/movingAverage.m +++ b/movingAverage.m @@ -1,11 +1,12 @@ function [outputSignal] = movingAverage(inputSignal, M) - outputSignal = zeros(length(inputSignal)); + % Initialize output array with zeros. + outputSignal = zeros(1, length(inputSignal)); % Pad input with zeros at the begining. inputSignal = [zeros(1, M-1), inputSignal]; + % For each sample in input... for n = M:length(inputSignal) - n - M + % Take the last M samples and save their mean value as the output sample. outputSignal(n-M+1) = mean(inputSignal(n-M+1:n)); end diff --git a/plotPeak.m b/plotPeak.m deleted file mode 100644 index 8df4595..0000000 --- a/plotPeak.m +++ /dev/null @@ -1,16 +0,0 @@ -% a function to plot -function plotPeak(peak_period, wave_height, timestamps) - figure - subplot(2,1,1) - plot(timestamps, wave_height); - title('Graph of wave height as function of time') - xlabel('Time') - ylabel('Wave Height') - datetick('x', 0) - - subplot(2,1,2) - plot(timestamps, peak_period); - title('Graph of peak period as function of time') - xlabel('Time') - ylabel('Peak Period') - datetick('x', 0) diff --git a/plotPeakWave.m b/plotPeakWave.m new file mode 100644 index 0000000..0362c88 --- /dev/null +++ b/plotPeakWave.m @@ -0,0 +1,26 @@ +% a function to plot +function plotPeakWave(peak_period, wave_height, timestamps) + % Create a new figure window + figure + % Split the figure into two seperate graphs and set preceeding plot + % functions to work on top graph. + subplot(2,1,1) + % Plot time data to x axis and wave height data to y axis. + plot(timestamps, wave_height); + % Add a title to the graph + title('Graph of wave height as function of time') + % Label the x axis as 'time' + xlabel('Time') + % Label the y axis as 'wave height' + ylabel('Significant Wave Height (m)') + % format time data to use 'dd-mmm-yyyy HH:MM:SS' + datetick('x', 0) + + % Set future plot functions to act on second plot + subplot(2,1,2) + % Same as above, but with peak period in place of wave height... + plot(timestamps, peak_period); + title('Graph of peak period as function of time') + xlabel('Time') + ylabel('Peak Period (s)') + datetick('x', 0)