You are on page 1of 6

01/05/2017 ENGG1811 Assignment 1 (Semester 1, 2017)

ENGG1811Assignment1:Detectingabnormalevents

Duedate:5pm,Friday5thMay2017(week9).Apartfromafewminutes'grace,late
submissionswillbepenalisedattherateof15%perday.Thepenaltyappliestothemaximum
availablemark.SubmissionswillgenerallynotbeacceptedafterWednesday10May2017.

(Version1.0,10/April/2017)

Thedetectionofabnormalevent
Inthisassignment,yourgoalistowriteanOOBasicprogramtodetectwhetherthereareabnormaleventsinthedata.This
typeofproblemisverycommoninmanydisciplines,includingcomputerscience,engineering,medicine,scienceand
business.Togiveafewexamples:

Ifacarshakesmorethanusual,thismaymeanthatcertaincomponentsarenotworkingcorrectly
Ageophysicistwantstofindoutwhetheranearthquakehasoccurredbycheckingwhetherthegroundvibrationis
greaterthannormal
Aseizureinthebrainoccurswhentheneuronsfireatthesametimeandatahigherrate
Anabnormalbusinesstransactionmaybeafraud

Giventhattherearesuchvarietiesofabnormalevents,itisnotsurprisingthattherearemanydifferenttypesofalgorithms
fordetectingabnormalevents.Inthisassignment,youwillbeprogramminganalgorithmthatthegeophysicistsusetodetect
whetheranearthquakehasoccurred.However,thesamealgorithmcanbeusedinmanyotherdisciplines,say,todetecta
largerthannormalvibrationortodetectwhetherafaulthasdevelopedinasystem.

Tipsforthisassignment(lectureslides)

Learningobjectives
Bycompletingthisassignment,youwilllearn:

1.Toapplybasicprogrammingconceptsofvariabledeclaration,constantdeclaration,assignment,conditional,while
loopandforloop.
2.Totranslateanalgorithmdescribedinanaturallanguagetoacomputerlanguage.
3.Toorganizeprogramsintomodulesbyusingfunctionsandsubprograms
4.Tousegoodprogramstyleincludingcomments,indentationandothers.
5.Togetapracticeonsoftwaredevelopment,whichincludesincrementaldevelopment,testinganddebugging.

Algorithmfordetectingabnormalevents
WehaveprovidedyouwithanOpenOfficeCalcworkbookassignment_template.ods.Thisworkbookcontainsmany
worksheetswhichyoucanusetotestyourprogramlateron.WewillusetheworksheetExampletoexplainwhatthe
algorithmis.Youcangetthedetailsoftheoriginalalgorithmfrom[1]andreadacasestudyofusingthisalgorithmto
monitorseismicactivitiesin[2].NotethatIhavesimplifiedandmodifiedtheoriginalalgorithmtomakethisassignmentless
complex.However,theintuitionremainsthesame.Iwillexplainthealgorithmintermsofearthquakebecauseitiseasierto
relateto,butthealgorithmcanalsobeappliedtodetectotherabnormalevents.

Itisimportanttopointoutthatthedescriptioninthissectionassumestheproblemparametersaregivenbythoseinthe
worksheetExample.However,yourOOBasicprogrammustbeabletodealwiththegeneralsetting,seeimplementation
requirementsbelowfordetails.

ColumnAoftheworksheetExamplecontainsadataserieswith50datapoints.YoucanthinkaboutanumberinColumnA
astheamplitudeofgroundvibrationatacertainpointintime.Alargernumbermeansthegroundisshakingmore.Ifweplot
thedata(seethegraphbelow),youfindthattheamplitudeisespeciallylargeinthemiddleportionofthedatasequence.

http://www.cse.unsw.edu.au/~en1811/17s1/assigns/ass1/ass1.html 1/6
01/05/2017 ENGG1811 Assignment 1 (Semester 1, 2017)

Oureyesandbrainareespeciallygoodatdetectingpatterns.Computerscanalsodetectpatternsbuttheydosoina
differentway.Soletusseehowanalgorithmcandoit.Theaimofthealgorithmistodistinguishbetweentwosituations:

1.Theusualgroundmovement
2.Earthquake

Theusualgroundmovementcanbethoughtofastheusualbehaviour,i.e.whatwehavebeenseeingforalongperiodof
time.Inthealgorithmwewillcapturetheusualbehaviourbyusinglongtermaverage(LTA),whichmeansanaverageof
thedatacomputedoveralongperiodoftime.

Anearthquakecanbethoughtofasaviolentshakingofthegroundoverashortperiodoftime.Inthealgorithm,wewill
capturethisbyusingshorttermaverage(STA).

WeexpecttheLTAtobesmallbecausethegroundisnotshakingthatmuchusually.Incontrast,weexpectwhenan
earthquakeoccurs,theSTAislargebecauseitisviolentshaking.Therefore,anindicationthatanearthquakehasoccurred
isthattheSTAismuchlargerthantheLTA.

WewillnowusethedataintheworksheetExampletoshowhowthealgorithmworks.Itisimportanttonotethatwewill
beusingspreadsheetnotationtodescribethealgorithmhere,buttheactualworkshouldbedoneinOOBasic.

ColumnAcontainsthedata.ThefirstdatapointisinRow2andtheendofthedataseriesisindicatedbyanemptycell.

InColumnB,wewanttocomputetheLTA.ThenumberofdatapointsthatyouwillusetocomputeLTAisgiveninCellH3.
ThisparameterisknownasLTAwindow.Forthisexample,theLTAwindowhasthevalueof10whichmeanswewill
computeLTAover10datapoints.Wewanttoperformthefollowing:

ThefirstLTAthatwecancomputeisoverthefirst10datapointsinColumnA,i.e.CellsA2tillA11.Intermsofcell
reference,wecomputeaverage(A2:A11)andplacetheanswerinCellB11.ThereasonwhyweplacetheresultinCell
B11isthat,atthetimethedatainA11iscollected,weusethe10datapointsuptoandincludingA11tocomputethe
LTA.
ThesecondLTAthatwecancomputeisfromthe2ndto11thdatapoints,i.e.thevaluesinCellsA3tillA12.Interms
ofcellreference,wecomputeaverage(A3:A12)andplacetheanswerinCellB12.
WerepeatthisuntilwegettothelastrowofdatainColumnA,i.e.Row51.WewillbecomputingtheLTAusingthe
last10datapoints,whichareinCellsA42toA51.TheaverageofCellsA42toA51shouldbeplacedinCellB51.

InColumnC,wewanttocomputetheSTA.ThenumberofdatapointsthatyouwillusetocomputeSTAisgiveninCellH4.
ThisparameterisknownasSTAwindow.Forthisexample,theSTAwindowhasthevalueof3whichmeanswewill
computeLTAover3datapoints.ThecomputationofSTAisanalogoustoLTAexceptthatSTAwindowisalwayssmaller
thanLTAwindow.Theexpectedresultsare:

CellsC2andC3areemptybecausethereisnotenoughdatainthebeginning
CellC4shouldcontaintheaverageofCellsA2toA4
CellC5shouldcontaintheaverageofCellsA3toA5
...
CellC51shouldcontaintheaverageofCellsA49toA51

AfterobtainingtheLTAandSTA,wewanttocomputetheratioSTA/LTA(i.e.STAdividedbyLTA).Wewillplacethisin
ColumnD.NotethatwecanonlycomputethisratiowhenbothLTAandSTAexist.Specifically,forthisexample,wehave

http://www.cse.unsw.edu.au/~en1811/17s1/assigns/ass1/ass1.html 2/6
01/05/2017 ENGG1811 Assignment 1 (Semester 1, 2017)
CellD11hasthevalueofC11/B11
CellD12hasthevalueofC12/B12
...
CellD51hasthevalueofC51/B51
Note:CellsD2toD10shouldbeempty

Inordertodecidewhetheranearthquakehasoccurred,wecomparetheSTAtoLTAratioagainstathreshold.The
thresholdisaparameterandisgiveninCellH5.Forthisexample,thethresholdis2.2.IftheSTAtoLTAratiocrossesfrom
avaluebelowthethresholdtoavalueabovethethreshold,thenwesaythatanearthquakeisdetectedatthatpointin
time.IntheworksheetExample,CellD11hasthevalueof1.11(belowthethreshold)andCellD12hasavalueof2.33
(abovethethreshold).Anearthquakeisdetectedandweput"Detected"inCellE12.Notethatinthisdataseries,the
earthquakeisonlydetectedonce,butyoumaybegivendataserieswheretherearemorethanoneearthquake.

Thenumberofabnormalevents(inourexample,earthquakes)shouldbewrittentoCellH9.Therequirementisthatifthere
isatleastoneabnormalevent,youputthenumberofabnormaleventsinCellH9.So,forthisexample,CellH9should
containthenumber1.Iftherearenoabnormalevents,youshouldputtheexpression"AllGood"inCellH9.

Thiscompletesthedescriptionexceptforonedetail.Weshouldonlyproceedwiththecalculationsdescribedaboveifwe
aregivensensibledataandparameterstostartwith.Youareexpectedtoperformthefollowingthreechecks.Allthesethree
checksmustpassbeforeyoushouldproceedwiththecalculations.Ifsomechecksarenotpassed,anerrormessage
shouldbedisplayedinCellH9andtheprogramisthenterminated.Thethreechecksare:

1.ThevalueofLTAwindowmustbestrictlylargerthanthevalueofSTAwindow.Theerrormessageis
"WrongWindowSizes".
2.TheremustbeenoughdatainColumnAtodothecalculations.WesaythatColumnAcontainsenoughdataifitis
possibletocalculateatleasttwoLTAs.Letusillustratethisbyanexample.LetusassumethatLTAwindowis10.If
thereare11datapointsormore,youcancomputeatleast2LTAs,sothereareenoughdata.Ifthereare10data
pointsorless,therearenotenoughdata.Theerrormessageis"NotEnoughData".
3.Thevalueofthresholdmustbestrictlygreaterthan1.Theerrormessageis"ThresholdTooSmall".

Youarerequiredtodothechecksintheordergivenabove.Onceyoufindacheckhasfailed,youdonothaveperformthe
otherchecksfurtherdownthelistyoushoulddisplaytheerrormessageforthefirstcheckthatfailsandthenterminatethe
program.

Implementationrequirements
YouarerequiredtoimplementthealgorithmfordetectingabnormaleventsdescribedaboveusingOOBasic.Anexecution
ofyourOOBasicprogram,bycallingthesubprogramAbnormalEventDetection(),shouldreturn

1.Oneoftheerrormessages"WrongWindowSizes","NotEnoughData"or"ThresholdTooSmall"ifyoucannotproceedto
doanycalculations.
2.Ifyoucanproceedtodocalculations,youshouldhave
a.LTAsinColumnB
b.STAsinColumnC
c.STAtoLTAratiosinColumnD
d.Theword"Detected"inColumnEeachtimeanabnormaleventisdetectedatthattimeinstance(row).Wesay
thatanabnormaleventisdetectedatarowifthevalueofSTALTAratioatthatrowisgreaterthanthe
thresholdwhilethevalueoftheSTALTAratioatthepreviousrowislowerthanthethreshold.
e.ThenumberofabnormaleventsinCellH9(strictlypositivenumberofevents)or"AllGood"(zeroabnormal
events).Note:ThenumberofabnormaleventsinCellH9shouldbethesameasthenumberoftimestheword
"Detected"appearsinColumnE.

YoucanassumealltheworksheetsthatwewillusetotestyourOOBasicprogramhavethesameformatastheworksheet
Example.Youcanalsomakethefollowingassumptions:

1.DataseriesisalwaysincolumnA.ThefirstdatapointisinCellA2.Anemptycellisusedtoindicatetheendofthe
data.Datacancontaindecimalpoints.
2.Theparametercells(H3,H4andH5)arealwaysnonempty.CellsH3andH4(containingLTAandSTAwindows)
alwayscontainpositiveintegers.CellH5(threshold)isapositiverealnumber.

IntheworksheetExample,thelengthofthedataseriesis50andtheparametervaluesare10,3and2.2.Youshouldnot
assumethatthesearethevaluesthatwewilluse.Youwillneedtodeterminethedatalengthandreadtheparametervalues
fromthecells.Wewillusevariousdatalengthsandparametervaluestotestyourprogram.

Yourimplementationmustincludetwofunctionsandasubprogramthatweprescribedbelow.Wewilltesttheseprocedures
independently.Notethattheseproceduresarebuildingblocksforyourprogramandyoushouldmakeuseofthem.Thisis
alsotherecommendedpracticeinsoftwaredevelopmentwhereyouidentifythebasicfunctionalblocksofyourprogramand
implementtheminfunctions/subprograms.

Allyourcodemustbeimplementedinthemodule"Detection".

Threeproceduresthatyoumustimplement
Yourimplementationshouldincludethefollowingthreeprocedures(twofunctionsandasubprogram).Pleasenotethatyou
mustnotchange(add/remove/changetheorder)theinputsfromanyofthefollowingprescribedprocedure.

http://www.cse.unsw.edu.au/~en1811/17s1/assigns/ass1/ass1.html 3/6
01/05/2017 ENGG1811 Assignment 1 (Semester 1, 2017)
1. Function FindNumberOfDataPoints() As Long

TheaimofthisfunctionistodeterminethenumberofdatapointsinColumnA
YouneedtomakeuseoftheconstantsDATA_ROW_START(=2)andDATA_COL(=1)inthisfunction.
Specifically,thefunctionusestheinformationthatthefirstdatapointisinrowDATA_ROW_START(=2)andin
thedataareincolumnDATA_COL(=1).
Thisfunctioncanbetestedusingtheworksheet"Test(FindNumberDataPoints)".

2. Function ComputeAverage(rstRow As Long, lastRow As Long) As Double

TheaimofthisfunctionistocalculatetheaverageofasectionofdatainColumnA
Forexample,acallofComputeAverage(4,10)inyourOOBasicprogramshouldreturntheaverageofthecells
fromA4toA10
YoucanmakeuseoftheconstantDATA_COL(=1)inthisfunctiontorefertoColumnA
ThefirstinputrstRowistherownumberofthestartingrow
ThesecondinputlastRowistherownumberofthelastrow
Thisfunctioncanbetestedusingtheworksheet"Test(ComputeAverage)".

3. Sub ComputeLSTermAverage(windowSize As Long, dataLength As Long, writeToCol As Long)

NotethatthecalculationsofLTAsandSTAsareverysimilar.Thedifferencesare:
LTAusesLTAwindowastheparameterwhileSTAusesSTAwindow
TheLTAsarewrittentoColumnBwhileSTAsarewrittentoColumnC
ThestartingrowthatyoucalculateLTAandSTAaredifferent
Sincethesecalculationsaresosimilar,onesubprogramcanbeusedtodothecalculationbypassingdifferent
parameterstothesub.Thisfitswellwiththephilosophyofmodulardesigninsoftware.
TheinputwindowSizeisthenumberofdatapointsforcalculatingtheaverage.IfyouwanttocalculateSTA,
youshouldusethevalueofSTAwindow.IfyouwanttocalculateLTA,youshouldusethevalueofLTAwindow.
TheinputdataLengthspecifiesthenumberofdatapointsinColumnA.Thisshouldbethesameastheoutput
ofthefunctionFindNumberOfDataPoints.
TheinputwriteToColspecifiesthedestinationcolumnthattheaveragesaretobewrittento.ForLTAaverages
andSTAaveragesshouldbewritteninColumnsBandCrespectively.
Toillustratewhatthissubshouldachieve,letususetheworksheetExample.
AcallofthissubwithComputeLSTermAverage(10, 50, 2)willfillinCellsB11toB51withtheLTA
averages,i.e.41averagesaltogether
AcallofthissubwithComputeLSTermAverage(3, 50, 3)willfillinCellsC4toC51withtheSTA
averages,i.e.48averagesaltogether
Note:Ihaveusedexplicitconstantsforillustration.Youshouldbeusingvariableswhencallingthissubin
yourprogram.
TheworksheetTest(LTA_STA)allowsyoutotestyoursubComptueLSTermAverage
Ifyoupressthebutton"CalculateLTA",itwillcallthetestroutine"TestLTACalculation()"
Similarly,pressingthebutton"CalculateSTA"willcallthetestroutine"TestSTACalculation()"(inthe
ModuleSupport)
Thecodeforthesetwotestproceduresisinthemodule.Youcanlookatthecodetoseewhatitdoes.It
willalsogiveyousomehintonhowyoucancallthesubComputeLSTermAverage yourprogram.
ColumnsJandKofthisworksheetcontaintheexpectedvaluesofLTAsandSTAs.ColumnsNandO
computethedifferencebetweentheexpectedvalueswithwhatyouhaveinColumnsBandC.Columns
NandOshouldbezeroifyourLTAandSTAcalculationsarecorrect.

GettingStarted
1.Downloadthetemplateworkbook.
2.Renameitass1_ab.ods
Thenameisdifferenttoavoidpossiblyoverwritingyourworkifyouweretodownloadthetemplateagainlater.The
workbookyousubmitmusthavethisname.
3.OpentheOOBasiceditorandlookattheDetectionmodule.Notealltheconstants.Alwaysusethesupplied
constants,yourprogramshouldnothaveanyliteralnumbersinitexceptforobviousvalueslike0,1or2.TheStyle
Assessorwillcheckthis.
ThesubnamemustremainasAbnormalEventDetection()donotchangethat.
4.Agoodstartingpointistoimplementthetwofunctionsandthesubbecausetheyhavebeenspecifiedforyou.The
suppliedworkbookhasworksheetsthatyoucanusetotestthesefunctions/sub.Onceyouhavegotthefunctions/sub
working,youcanmakeuseofthemtobuildyourcompleteprogram.Donottrytoimplementtoomuchatonce,just
onepartoftheansweratatimeandtestthatitisworkingbeforemovingon.
5.PleasedonotuseMsgBoxoranyotherprocedurethatcreateswindows.Wewon'tbeabletoassessyourprogram
properlyifyoudo.Remember,allresponsesshouldbewrittentotheactivesheet.

Testing
Testyourfunctionsthoroughlybeforesubmission.Theworkbookcontainsmanyworksheetsforyoutotestyourprogram.

WehavealreadymentionedtheworksheetsTest(FindNumberOfDataPoints),Test(ComputeAverage)and
Test(LTA_STA)whichyoucanusetotesttheprocedures.
YoucanusetheworksheetsExample,Test1,Test2,Test3andTest4totestyourentireprogram.Ifyouclickthebutton
"RunDetectionAlgorithm"ontheworksheet,itwillautomaticallyexecutethesubAbnormalEventDetection()foryou.

http://www.cse.unsw.edu.au/~en1811/17s1/assigns/ass1/ass1.html 4/6
01/05/2017 ENGG1811 Assignment 1 (Semester 1, 2017)
(YoucanofcourseuseRunMacrostorunthesubAbnormalEventDetection()ifyouwish.)Ifyouprogramruns
correctly,itshouldfillinColumnsBtoE,aswellasCellH9appropriately.
TheworksheetExample_Completedshowsthecorrectanswers.SimilarlyforTest1_Completedetc.
ColumnsJLoftheseworksheetscontaintheexpectedvaluesofLTA,STAandLTASTAratio.ColumnsNP
computethedifferencebetweentheexpectedvalueswithwhatyouhaveinColumnsBD.ColumnsNtoP
shouldbezeroifyourcalculationsarecorrect.
TheworksheetsTest(NotEnoughData)andTest(WrongWindowSizes)canbeusedtotesttheerrors
"NotEnoughData"and"WrongWindowSizes".Wehaven'tprovidedatestworksheetfor"ThresholdTooSmall"butyou
caneasilymakeoneyourselves.
Pressingthebutton"ClearCells"willclearthecellsinColumnsBEandCellH9.

Wehaveincludedtheseworksheetstoshowyouhowyoushouldbetestingyourprogram.Youareofcoursewelcometo
makeupyourowntests.

Submission

Pleasenotethatyouneedtosubmityoursolutionfilenamed"ass1_ab.ods",andallyourcodemustbe
implementedinthemodule"Detection".
InstructionsonhowtosubmitthisassignmentwillbepostedhereinWeek8.

AssessmentCriteria
Wewilltestyourprogramthoroughlyandobjectively.Thisassignmentwillbemarkedoutof25where20marksarefor
correctnessand5marksareforstyle.

Correctness

The20marksforcorrectnessareawardedaccordingtothesecriteria.

Nominal
Criteria
marks
Basicsanitychecksfor"NotEnoughData","WrongWindowSize","ThresholdTooSmall" 3
FunctionFindNumberOfDataPoints 2
FunctionComputeAverage 3
SubComputeLSTermAverage 4
CorrectSTAtoLTAratios 2
Correct"Detected"(numberandlocation)inColumnE 4
CorrectoutcomeforNumberofAbnormaleventsinCellH9(Eithernumberofabnormaleventsor
2
"AllGood")

Style

Five(5)marksareawardedbyaStyleAssessor.Thestyleassessmentincludethefollowing,innoparticularorder:

Useofproceduresforsubsidiarytasks(havingsufficientofthem,andnonetoocomplex)
Capitalisedkeywords(Listofkeywords:And,As,Const,End,Else,Exit,For,Function,If,Next,Not,Or,Sub,Then,
While,Wend)
Indentingtoreflecttheprogramstructure
Useofthesuppliedconstants(andanyothersthatyoumightneed)
Useofmeaningfulvariableandprocedurenameswhereapplicable
Useofsensiblecommentstoexplainwhatyou'redoing
Identificationofauthor,developmentdatesandanyreferencesinacommentatthetopofthemodule

YoucanusetheStyleAssessortocheckandimproveyourstyle.TheAssessorisavailableviatheStyleAssessorlinkon
theclasspage(intheleftpanel).Youneedtouploadyourfileforstyleassessmentanditwillprovideyouwithareportand
informyouhowyoumayimproveyourscore.

TheStyleAssessorwillgiveyouascoreoutof5,roundeddowntothenearest0.5.Thefollowingtableshowsyouhowwe
willmaptheStyleAssessorscore(s)totheactualmark(m):

StyleAssessorscores(outof5) Markawardedforstylem(outof5)
s=4.5or5 m=5
s=4 m=4.5
s<4 m=s

http://www.cse.unsw.edu.au/~en1811/17s1/assigns/ass1/ass1.html 5/6
01/05/2017 ENGG1811 Assignment 1 (Semester 1, 2017)
Thismeansthatyoudonotneedtoaimforascoreof5/5forStyleAssessortogetthefullmarkforstyle.Notethatdueto
thewaythattheStyleAssessorworks,itmaytakealotofefforttoimprovethescorefrom4.5/5to5/5,sotheabove
mappingissayingthat4.5/5forStyleAssessorisgoodenough.

AssignmentOriginality
Youareremindedthatworksubmittedforassessmentmustbeyourown.ItisOKtodiscussapproachestosolutionswith
otherstudents,andtogethelpfromtutorsandconsultants,butyoumustwritetheBasiccodeyourself.Sophisticated
softwareisusedtoidentifysubmissionsthatareunreasonablysimilar,andmarkswillbereducedorremovedinsuchcases.

FurtherInformation
Usetheforumtoaskgeneralquestionsabouttheassignment,andkeepaneyeonitforupdatesandresponses.

ConsultantswillbeavailableduringWeeks79.Usetheforumtoaskgeneralquestionsabouttheassignment,buttake
specificonestotheconsultants.

References:

[1]E.T.EndoandT.L.Murray.RealtimeSeismicAmplitudeMeasurement(RSAM):avolcanomonitoringandprediction
tool.BulletinofVolcanology,53:533545,1991.

[2]W.Z.Song,R.Huang,M.Xu,A.Ma,B.Shirazi,andR.Lahusen.AirdroppedSensorNetworkforRealtimeHighfidelity
VolcanoMonitoring.InThe7thAnnualInternationalConferenceonMobileSystems,ApplicationsandServices(MobiSys),
June2009.

http://www.cse.unsw.edu.au/~en1811/17s1/assigns/ass1/ass1.html 6/6

You might also like