Advertisement
PT_

Untitled

PT_
Apr 17th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. close all
  2. load output5.dat;
  3. optfreq2;
  4. data2 = output5;
  5. %% DETERMINING DATES
  6.  
  7. startDay = '13'; %Edit desired date and length of sample here
  8. startMonth = 'Dec';
  9. startYear = 2014;
  10.  
  11. endDay = '1';
  12. endMonth = 'Jan';
  13. endYear = 2016;
  14.  
  15. predDay = '1';
  16. predMonth = 'Jan';
  17. predYear = 2017;
  18.  
  19. startDay = '10'; %Edit desired date and length of sample here
  20. startMonth = 'Feb';
  21. startYear = 2016;
  22.  
  23. endDay = '27';
  24. endMonth = 'Feb';
  25. endYear = 2017;
  26.  
  27. predDay = '1';
  28. predMonth = 'May';
  29. predYear = 2017;
  30.  
  31. singlepredDay = '20';
  32. singlepredMonth = 'Apr';
  33. singlepredYear = 2017;
  34.  
  35. month = strcat(startDay, '-', startMonth, '-');
  36. startDate = (datenum(strcat(month, num2str(startYear+0)))-725738)*24*6;
  37.  
  38. endmonth = strcat(endDay, '-', endMonth, '-');
  39. endDate = (datenum(strcat(endmonth, num2str(endYear+0)))-725738)*24*6;
  40.  
  41. predNum = strcat(predDay, '-', predMonth, '-');
  42. predDate = (datenum(strcat(predNum, num2str(predYear+0)))-725738)*24*6;
  43.  
  44. singlepredNum = strcat(singlepredDay, '-', singlepredMonth, '-');
  45. singlepredDate = (datenum(strcat(singlepredNum, num2str(singlepredYear+0)))-725738)*24*6;
  46.  
  47. %% DEFINING IMPORTANT VARIABLES
  48.  
  49. fourierVec = data2(startDate:endDate);
  50.  
  51. if predDate < length(data2)
  52. predVec = data2(endDate:predDate);
  53. end
  54.  
  55. predLength = length([endDate:predDate]);
  56.  
  57. lfV = length(fourierVec);
  58.  
  59. unit = 1/(6*24);
  60. t = 0:unit:(lfV-1)*unit;
  61.  
  62. ampFilterMax = 200; %Edit desired amplitude filtering here
  63.  
  64. frequencyDomain = 0: 1/(length(fourierVec)/(6*24)) : (length(fourierVec)-1)/(length(fourierVec)/(6*24));
  65.  
  66. %% CALCULATING FOURIER TRANSFORM AND FREQUENCIES
  67.  
  68. figure
  69.  
  70. fourierTransform = fft(fourierVec*24*6/lfV); %Creating and filtering the Fourier Transform
  71.  
  72. %% METHOD 1 (COMMENT ONE)
  73.  
  74. % fourierTransform(abs(fourierTransform) < ampFilterMax) = 0;
  75. % freqVec = find(abs(fourierTransform) > 0);
  76. % realfreqVec1 = [];
  77. %
  78. % for i =1:length(freqVec)
  79. % if(freqVec(i) < (1/2)*lfV)
  80. % realfreqVec1 = [realfreqVec1 2*pi*freqVec(i)/(lfV*unit)];
  81. % end
  82. % end
  83.  
  84. %% METHOD 2 (COMMENT ONE)
  85.  
  86. % [peaks, freqVecAll] = findpeaks(abs(fourierTransform), 'MinPeakHeight', 100);
  87. % freqVecAll = 2*pi*freqVecAll/(lfV*unit);
  88. % realfreqVec2 = freqVecAll(1:1/2*length(freqVecAll))';
  89.  
  90. %%
  91.  
  92. realfreqVec = optfreq3;
  93.  
  94. display(length(realfreqVec));
  95. absFourier = abs(fourierTransform);
  96.  
  97. %% MAKING APPROXIMATION
  98.  
  99. f=fourierVec-mean(fourierVec);
  100. abSolve = [];
  101. for i = 1:length(realfreqVec)
  102. abSolve=[abSolve; fminsearch(@(x) sum((f(1:length(f))' -x(1)*cos(realfreqVec(i)*t(1:length(f))) - x(2)*sin(realfreqVec(i)*t(1:length(f)))).^2),[-100,-100])];
  103. display(i)
  104. end
  105.  
  106. a=[];
  107. b=[];
  108.  
  109. for i=1:length(realfreqVec)
  110. a = [a abSolve(i,1)];
  111. b = [b abSolve(i,2)];
  112. end
  113.  
  114. sumCosApprox = 0;
  115.  
  116. for i=1:length(a)
  117. sumCosApprox = sumCosApprox +a(i)*cos(realfreqVec(i)*t)+b(i)*sin(realfreqVec(i)*t);
  118. end
  119. sumCosApprox = sumCosApprox+mean(fourierVec);
  120.  
  121. t = 0:unit:(predLength-1)*unit;
  122. sumCos = 0;
  123.  
  124. for i=1:length(a)
  125. sumCos = sumCos +a(i)*cos(realfreqVec(i)*t)+b(i)*sin(realfreqVec(i)*t);
  126. end
  127. sumCos = sumCos+mean(fourierVec);
  128. %% PLOTTING THINGS
  129.  
  130. plot(frequencyDomain, absFourier);
  131. axis([0 10 0 max(absFourier)]);
  132. title('Filtered Fourier Transform');
  133. xlabel('1/T');
  134.  
  135. figure
  136.  
  137. plot(data2(startDate:endDate), 'Color', 'r');
  138. hold on
  139. plot(sumCosApprox, 'Color', 'b')
  140. title('Approximation using sines and cosines');
  141. legend('Original Data', 'Approximation');
  142. xlabel('Index of approximated data points');
  143. ylabel('Water height in cm above NAP');
  144. if max(data2)> max(sumCosApprox)
  145. if min(data2) < min(sumCosApprox)
  146. axis([0 lfV min(data2) max(data2)]);
  147. else
  148. axis([0 lfV min(sumCosApprox) max(data2)]);
  149. end
  150. else
  151. if min(data2) > min(sumCosApprox)
  152. axis([0 lfV min(sumCosApprox) max(sumCosApprox)]);
  153. else
  154. axis([0 lfV min(data2) max(sumCosApprox)]);
  155. end
  156. end
  157. %% PREDICTING KNOWN DATA
  158. errorSumCos = sumCos;
  159.  
  160. if predDate < length(data2)
  161. predVec = data2(endDate:predDate);
  162. errorApproximation = errorSumCos(1:length(errorSumCos)) - predVec(1:length(predVec))';
  163.  
  164. figure
  165.  
  166. plot(predVec);
  167. hold on
  168. plot(errorSumCos);
  169.  
  170. axis([0 predLength min(predVec) max(predVec)])
  171. title('Prediction of known data');
  172. xlabel('Index of predicted data points');
  173. ylabel('Predicted water height in cm above NAP');
  174. legend('Original Data','Prediction');
  175.  
  176. figure
  177. plot(errorApproximation);
  178. axis([0 predLength min(errorApproximation) max(errorApproximation)]);
  179. title('Error of predicting known data');
  180. xlabel('Index of predicted data');
  181. ylabel('Error in cm of predicted data')
  182.  
  183. predictionTime = sumCos;
  184. predictionTime(find(sumCos<=160))=0;
  185. predictionTime(find(sumCos>160))=1;
  186.  
  187. dataTime = data2(endDate:predDate);
  188.  
  189. dataTime(find(dataTime<=150))=0;
  190. dataTime(find(dataTime>150))=1;
  191.  
  192. figure
  193.  
  194. predictionMistake = predictionTime(1:predLength)-dataTime(1:predLength)';
  195. plot(predictionMistake);
  196. axis([0 predLength -1.5 1.5]);
  197. title('Prediction accuracy of known data');
  198. xlabel('Index of predicted data points');
  199. ylabel('Error in prediction');
  200.  
  201. predErrors = find(predictionMistake ~= 0);
  202. percentageWrong = length(predErrors)/predLength;
  203. display(percentageWrong);
  204.  
  205. predErrorsUp = find(predictionMistake > 0);
  206. percentageUp = length(predErrorsUp)/predLength;
  207. display(percentageUp);
  208.  
  209. predErrorsDown = find(predictionMistake < 0);
  210. percentageDown = length(predErrorsDown)/predLength;
  211. display(percentageDown);
  212.  
  213. figure
  214.  
  215. errorMean = mean(abs(errorApproximation));
  216. errorDeviation = std(errorApproximation);
  217. display(errorMean);
  218. display(errorDeviation);
  219. histogram(errorApproximation);
  220. title('Distribution of error of known data');
  221. xlabel('Error in cm');
  222. ylabel('Amount of data points');
  223.  
  224. figure
  225.  
  226. [F, y] = ecdf(errorApproximation);
  227. plot(y, F);
  228. title('Cumulative Distribution of error of known data');
  229. xlabel('Error in cm');
  230. ylabel('Cumulative probability');
  231.  
  232. error = [];
  233. approx = [];
  234. for i=1:length(predVec)
  235. if predVec(i) >= 150
  236. approx = [approx errorSumCos(i)];
  237. error = [error errorApproximation(i)];
  238. end
  239. end
  240. figure
  241. histogram(error);
  242. title('Distribution of error looking at prediction of high water levels');
  243. xlabel('Error in cm');
  244. ylabel('Amount of data points');
  245.  
  246. figure
  247.  
  248. [G, z] = ecdf(error);
  249. plot(z, G);
  250. title('Cumulative Distribution of prediction at high water levels');
  251. xlabel('Error in cm');
  252. ylabel('Cumulative probability');
  253.  
  254. end
  255.  
  256. %% PREDICTING UNKNOWN DATA
  257.  
  258. if predDate > length(data2)
  259.  
  260. figure
  261.  
  262. plot(errorSumCos);
  263. axis([0 predLength min(errorSumCos) max(errorSumCos)]);
  264. title('Prediction');
  265. %axis([0 predictionLength min(errorSumCos) max(errorSumCos)]);
  266. xlabel('Index of predicted data points');
  267. ylabel('Predicted water height in cm above NAP');
  268.  
  269. prediction_150 = errorSumCos;
  270. prediction_150(find(errorSumCos > 150)) = 1;
  271. prediction_150(find(errorSumCos <= 150)) = 0;
  272.  
  273. figure
  274.  
  275. plot(errorSumCos)
  276. axis([singlepredDate-endDate singlepredDate-endDate+24*6 min(errorSumCos) max(errorSumCos)]);
  277. title('Prediction of April 20th');
  278. xticks(singlepredDate-endDate:12:singlepredDate-endDate+24*6);
  279. xticklabels({'00:00', '02:00', '04:00', '06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00', '24:00'});
  280. xlabel('Time (hours)');
  281. ylabel('Predicted water height in cm above NAP');
  282.  
  283. figure
  284.  
  285. plot(prediction_150)
  286. axis([singlepredDate-endDate singlepredDate-endDate+24*6 -1.5 1.5]);
  287. xticks(singlepredDate-endDate:12:singlepredDate-endDate+24*6);
  288. xticklabels({'00:00', '02:00', '04:00', '06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00', '24:00'});
  289. title('Predicted intervals at which a ship can pass on April 20th');
  290. xlabel('Time (hours)');
  291. ylabel('Predicted water height in cm above NAP');
  292.  
  293. if length(F)>0
  294. figure
  295. plot(y,F)
  296.  
  297. figure
  298. errorplot = [];
  299. for i = 1:length(errorSumCos)
  300. errorplot(i) = max(F(y<=150-errorSumCos(i)));
  301. end
  302. plot(errorplot)
  303. hold on
  304. plot(prediction_150)
  305. axis([singlepredDate-endDate singlepredDate-endDate+24*6 0 1]);
  306. xticks(singlepredDate-endDate:12:singlepredDate-endDate+24*6);
  307. xticklabels({'00:00', '02:00', '04:00', '06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00', '24:00'});
  308. xlabel('Time (hours)');
  309. ylabel('Probability that a ship can pass through on April 20th');
  310. end
  311.  
  312. if length(G)>0
  313. figure
  314. plot(z,G)
  315.  
  316. figure
  317. errorplot = [];
  318. for i = 1:length(errorSumCos)
  319. errorplot(i) = max(G(z<=150-errorSumCos(i)));
  320. end
  321. plot(errorplot)
  322. hold on
  323. plot(prediction_150)
  324. axis([singlepredDate-endDate singlepredDate-endDate+24*6 0 1]);
  325. xticks(singlepredDate-endDate:12:singlepredDate-endDate+24*6);
  326. xticklabels({'00:00', '02:00', '04:00', '06:00', '08:00', '10:00', '12:00', '14:00', '16:00', '18:00', '20:00', '22:00', '24:00'});
  327. xlabel('Time (hours)');
  328. ylabel('Probability that a ship can pass through on April 30th - High points');
  329. end
  330. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement