Continued with exercises

This commit is contained in:
Sam Perry
2016-10-12 09:47:35 +01:00
parent 9360564f4f
commit 1703a3e368
4 changed files with 79 additions and 19 deletions
+49
View File
@@ -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)
+4 -3
View File
@@ -1,11 +1,12 @@
function [outputSignal] = movingAverage(inputSignal, M) 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. % Pad input with zeros at the begining.
inputSignal = [zeros(1, M-1), inputSignal]; inputSignal = [zeros(1, M-1), inputSignal];
% For each sample in input...
for n = M:length(inputSignal) for n = M:length(inputSignal)
n % Take the last M samples and save their mean value as the output sample.
M
outputSignal(n-M+1) = mean(inputSignal(n-M+1:n)); outputSignal(n-M+1) = mean(inputSignal(n-M+1:n));
end end
-16
View File
@@ -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)
+26
View File
@@ -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)