%% Laboratorní úloha č. 2 - Ukázka trojúhleníkového detektoru

% sample data
emg = [0.102083,0.102372,0.102615,0.102845,0.103076,0.103302,0.103512,0.103687,0.103808,0.103851,0.103795,0.103621,0.103312,0.102856,0.102243,0.101466,0.100522,0.0994104,0.0981352,0.0967006,0.0951135,0.0933823,0.0915166,0.089528,0.0874289,0.0852323,0.0829507,0.0805962,0.0781811,0.0757164,0.073213,0.0706813,0.0681298,0.0655657,0.0629953,0.0604243,0.0578579,0.0553014,0.0527591,0.0502339,0.0477274,0.0452398,0.0427716,0.0403222,0.0378908,0.0354758,0.0330749,0.0306865,0.0283099,0.0259403,0.0235591,0.0211278,0.0185996,0.015944,0.0131542,0.010239,0.00721666,0.00411246,0.000955649,0.00222343,0.0053945,0.00852893,0.0116015,0.0145909,0.0174797,0.0202544,0.0229044,0.0254234,0.0278083,0.030059,0.0321791,0.0341748,0.0360537,0.0378242,0.0394967,0.0410827,0.0425939,0.0440431,0.0454425,0.0468038,0.048138,0.0494558,0.0507675,0.0520818,0.0534068,0.0547493,0.0561151,0.0575091,0.0589349,0.0603959,0.0618956,0.0634359,0.0650183,0.0666438,0.0683125,0.0700236,0.0717751,0.0735634,0.0753845,0.0772335,0.0791053,0.0809953,0.082898,0.0848067,0.0867152,0.0886175,0.0905075,0.0923795,0.0942284,0.0960496,0.0978393,0.0995945,0.101312,0.102987,0.104615,0.106192,0.107715,0.109178,0.110579,0.111915,0.113184,0.114384,0.115514,0.116574,0.117564,0.118484,0.119334,0.120114,0.120825,0.121465,0.122036,0.122536,0.122964,0.12332,0.123601,0.123808,0.123939,0.123993,0.123971,0.123874,0.123702,0.123459,0.123147,0.122769,0.122328,0.121829,0.121274,0.120668,0.120014,0.119318,0.118582,0.11781,0.117006,0.116174,0.115318,0.11444,0.113547,0.112642,0.111731,0.11082,0.109912,0.109012,0.108123,0.107249,0.106392,0.105554,0.104733,0.103929,0.103137,0.102355,0.10158,0.100807,0.100032,0.0992532,0.0984662,0.0976678,0.0968549,0.0960249,0.0951757,0.0943062,0.0934182,0.092515,0.091601,0.0906814,0.0897638,0.0888564,0.0879679,0.0871079,0.0862866,0.085514,0.0847985,0.0841486,0.0835737,0.0830832,0.0826865,0.0823933,0.0822128,0.0821533,0.0822213,0.0824219,0.0827603,0.0832405,0.0838651,0.0846343,0.085546,0.0865962,0.0877795,0.0890895,0.0905197,0.0920624,0.0937091,0.0954497,0.0972735,0.0991705,0.10113,0.103139,0.105184,0.107252,0.109327,0.111398,0.113449,0.115468,0.117441,0.119353,0.121189,0.122935,0.124578,0.126108,0.127511,0.128779,0.129905,0.130882,0.131707,0.132375,0.132884,0.133233,0.133422,0.133453,0.133327,0.133051,0.132629,0.13207,0.131384,0.130579,0.129668,0.128661,0.127572,0.126412,0.125189,0.123916,0.1226,0.12125,0.119874,0.118477,0.117067,0.115649,0.114227,0.112806,0.111388,0.109977,0.108573,0.107177,0.105792,0.104417,0.103052,0.101697,0.10035,0.0990075,0.0976669,0.0963243,0.0949764,0.0936199,0.0922523,0.0908723,0.0894805,0.0880773,0.0866628,0.0852382,0.0838049,0.0823647,0.0809188,0.0794684,0.0780136,0.0765557,0.0750959,0.0736359,0.0721769,0.07072,0.0692657,0.067814,0.0663653,0.0649207,0.0634806,0.0620441,0.0606103,0.0591786,0.0577495,0.0563233,0.0549009,0.0534836,0.0520731,0.0506725,0.049287,0.0479236,0.046589,0.0452895,0.0440322,0.042824,0.0416717,0.0405818,0.0395599,0.038611,0.0377386,0.0369444,0.0362296,0.0355946,0.0350402,0.0345667,0.034173,0.0338568,0.0336153,0.0334456,0.0333449,0.0333103,0.0333383,0.0334257,0.0335693,0.0337651,0.0340084,0.0342939,0.0346157,0.0349673,0.035342,0.0357322,0.03613,0.0365276,0.0369176,0.037293,0.0376485,0.0379786,0.0382776,0.03854,0.0387601,0.0389327,0.0390541,0.039121,0.0391292,0.0390745,0.0389526,0.0387594,0.0384919,0.0381483,0.0377272,0.0372281,0.0366523,0.0360026,0.0352825,0.0344963,0.0336484,0.0327428,0.0317831,0.0307722,0.0297131,0.0286084,0.0274612,0.0262741,0.0250495,0.0237896,0.0224955,0.0211685,0.0198096,0.018419,0.016997,0.015544,0.0140603,0.0125477,0.0110095,0.00944897,0.00786976,0.00627592,0.00467118,0.0030589,0.00144216,0.00017685,0.0017967,0.0034166,0.00503686,0.00665817,0.00828149,0.00990748,0.0115359,0.0131671,0.0148021,0.0164428,0.0180916,0.0197519,0.0214279,0.0231232,0.0248405,0.0265814,0.0283474,0.0301403,0.031961,0.0338086,0.0356813,0.0375764,0.0394907,0.0414209,0.0433641,0.0453179,0.0472805,0.0492498,0.0512224,0.0531948,0.0551645,0.0571299,0.0590894,0.0610417,0.0629851,0.064917,0.0668343,0.0687337,0.0706127,0.0724688,0.074299,0.0760999,0.0778684,0.079603,0.0813025,0.0829648,0.084587,0.0861659,0.0876983,0.0891811,0.0906111,0.0919852,0.0933006,0.094555,0.095747,0.0968749,0.0979378,0.0989356,0.0998695,0.100741,0.101552,0.102303,0.102996,0.10363,0.104206,0.104722,0.105181,0.10558,0.105922,0.106207,0.106437,0.106613,0.106736,0.106808,0.106828,0.106798,0.106715,0.106582,0.106399,0.106167,0.105889,0.105568,0.105206,0.104804,0.104366,0.103894,0.103393,0.102867,0.102321,0.101763,0.101198,0.100632,0.100072,0.099526,0.0990001,0.0985014,0.0980377,0.0976174,0.0972498,0.096944,0.096709,0.0965525,0.0964824,0.0965067,0.0966344,0.0968744,0.0972363,0.0977317,0.0983742,0.0991783,0.100159,0.101332,0.102712,0.104316,0.106162,0.108264,0.11064,0.113307,0.116283,0.11959,0.123249,0.127285,0.131728,0.136613,0.14198,0.147876,0.154348,0.16145,0.169235,0.177753,0.187054,0.19718,0.208171,0.220059,0.23287,0.246628,0.26135,0.277049,0.293738,0.311422,0.330105,0.349788,0.37047,0.392148,0.414821,0.438484,0.463135,0.488769,0.51538,0.542966,0.571526,0.601059,0.63157,0.663062,0.695539,0.729008,0.763475,0.798946,0.835423,0.872905,0.911386,0.950857,0.9913,1.03269,1.07499,1.11818,1.1622,1.20701,1.25257,1.29881,1.34568,1.39314,1.44112,1.48955,1.53838,1.58754,1.63693,1.68649,1.73612,1.78575,1.83526,1.88458,1.9336,1.98223,2.03038,2.07793,2.1248,2.17089,2.21609,2.26031,2.30345,2.34539,2.38601,2.42514,2.46257,2.49807,2.53136,2.56214,2.59011,2.61497,2.63641,2.65414,2.66792,2.67751,2.68272,2.68343,2.67952,2.67096,2.65773,2.63988,2.6175,2.59072,2.55969,2.52461,2.4857,2.44322];
S = [0,1.205563,2.424673,3.6476115,4.8702555,6.094372,7.3232005,8.5623365,9.8173755,11.0953855,12.402551,13.7444675,15.1261415,16.551107,18.022309,19.541809,21.1104905,22.7285302,24.3947501,26.1079133,27.86598775,29.66649965,31.5066218,33.382938,35.29179645,37.22936865,39.19197335,41.1760471,43.17796755,45.1944952,47.22242,49.25867915,51.3007694,53.34657035,55.39422665,57.44205965,59.48853795,61.5321007,63.57148155,65.60582645,67.6346642,69.6579359,71.6754943,73.6875161,75.6942369,77.6961279,79.69386645,81.68792375,83.67850595,85.66702665,87.65896355,89.6656549,91.7008833,93.773631,95.8859006,98.0351005,100.21585313,102.42071353,104.6410678695,105.558379865,105.91517325,106.282757115,106.66855875,107.07885395,107.51877585,107.9923002,108.5025487,109.0513767,109.63969715,110.2675395,110.93384355,111.6367834,112.37412085,113.1433821,113.94150435,114.76510085,115.61072595,116.47461005,117.35316025,118.2429309,119.1406825,120.0432639,120.94764175,121.8512539,122.7517149,123.64702215,124.53546755,125.41560805,126.28638345,127.14679245,127.9958043,128.83285945,129.65751615,130.4694799,131.26872125,132.0554758,132.83033255,133.5943517,134.34871125,135.09485425,135.83428265,136.56835115,137.2986795,138.02724085,138.7558611,139.48630725,140.22037575,140.95974525,141.7059177,142.4602478,143.22385465,143.99762175,144.7824915,145.5798775,146.391105,147.217352,148.059502,148.919322,149.797401,150.6946225,151.6115755,152.548849,153.5067375,154.485241,155.4843595,156.504093,157.5444415,158.605405,159.686689,160.7888825,161.9113965,163.05482,164.2194475,165.405279,166.613198,167.84291,169.095004,170.3697745,171.666927,172.986167,174.3274945,175.6897315,177.072289,178.4742835,179.8948315,181.3324605,182.7865815,184.255722,185.7389985,187.234644,188.7420695,190.260097,191.7875485,193.323246,194.8660115,196.415256,197.968918,199.526114,201.085077,202.64404,204.2021195,205.757843,207.310327,208.8583935,210.4014535,211.938918,213.471376,214.9988275,216.522745,218.0437175,219.5626285,221.0809505,222.5998615,224.1198916,225.6423366,227.1681389,228.69821145,230.23331995,231.77408285,233.3208241,234.8730136,236.4296795,237.989526,239.5510217,241.1119284,242.6698312,244.22216795,245.76611145,247.2986578,248.816862,250.31825025,251.8003193,253.26030085,254.6954266,256.10292825,257.47994915,258.8237799,260.13197615,261.40262365,262.63422045,263.82523515,264.97448975,266.08121855,267.14536265,268.1675405,269.1489301,270.09112175,270.99600025,271.86547985,272.7018282,273.50754855,274.28561535,275.03917975,275.77118675,276.4847875,277.1838105,277.8722315,278.553879,279.233465,279.914229,280.600883,281.296961,282.006586,282.7341755,283.484147,284.2606235,285.0674335,285.907522,286.785012,287.7022595,288.661326,289.664273,290.711984,291.8059315,292.9467045,294.1345975,295.3696105,296.6511545,297.978935,299.3508905,300.765843,302.221142,303.7138425,305.2415885,306.8005515,308.3877865,309.9991705,311.631464,313.282311,314.947883,316.6261185,318.314367,320.0102725,321.7123625,323.418281,325.1265555,326.836008,328.545166,330.2534405,331.9596535,333.663805,335.3656005,337.0641565,338.7597675,340.4524335,342.1421545,343.8295195,345.51555925,347.20103945,348.88710865,350.5747387,352.26490145,353.95833315,355.65541665,357.35597525,359.05989115,360.7671349,362.4773531,364.19013345,365.90494585,367.6214369,369.3392532,371.0583653,372.77839035,374.49897495,376.21961845,377.93996745,379.659698,381.37866285,383.096862,384.81417765,386.53028585,388.2450688,389.95879155,391.67171915,393.3840283,395.09557175,396.80626115,398.51583145,400.2238998,401.92996555,403.63311575,405.331819,407.0240138,408.707727,410.38110325,412.0420516,413.68854,415.31856585,416.9302149,418.52183795,420.0919625,421.6395578,423.1641232,424.6653053,426.1429863,427.5969306,429.02704985,430.433668,431.8174624,433.17925765,434.5199078,435.84023745,437.14110065,438.42352815,439.68846235,440.93684565,442.16985605,443.3888777,444.59547145,445.79137485,446.97850215,448.1588265,449.3345861,450.5081075,451.6816878,452.8575063,454.0376245,455.22360325,456.4170623,457.6196803,458.833077,460.05893105,461.29877385,462.55369505,463.8246665,465.1129251,466.41970775,467.7462808,469.0938517,470.46330395,471.85516765,473.2698551,474.70751355,476.16776015,477.6497703,479.15251325,480.67472265,482.2151027,483.7724754,485.34578055,486.9341641,488.53674255,490.1527502,491.7812741,493.42154855,495.07286675,496.7345808,498.40636675,500.08784175,501.7787113,503.4789165,505.188369,506.906951,508.63457415,510.37070835,512.11438175,513.864631335,515.62038218,517.38044156,519.14371099,520.90920095,522.67600438,524.339311675,525.15293935,525.9665523,526.78005923,527.593256935,528.405862695,529.21768214,530.02878595,530.83907105,531.64823705,532.4557244,533.2608263,534.06254145,534.85963295,535.6510406,536.43596925,537.2139477,537.9845342,538.74719865,539.501676,540.2482313,540.98739465,541.7199612,542.44687335,543.16910295,543.88750405,544.60278345,545.31547125,546.0261859,546.7359287,547.4457304,548.15632725,548.86819045,549.5817912,550.29751235,551.01585455,551.7375835,552.46361215,553.19491235,553.93222035,554.6762724,555.427952,556.18826045,556.9581107,557.7379445,558.52811525,559.3292414,560.142177,560.96786445,561.80724615,562.66123505,563.53077355,564.4167746,565.3200628,566.2413155,567.180945,568.13945195,569.1171014,570.1139228,571.12956275,572.1635795,573.2154135,574.2849175,575.3715025,576.475463,577.5965045,578.735216,579.890714,581.063882,582.2538365,583.4605775,584.683516,585.9223575,587.1768075,588.446277,589.7310605,591.030569,592.345686,593.675528,595.020095,596.3790925,597.751637,599.136845,600.5341275,601.94319,603.3628545,604.792532,606.23075,607.6763305,609.127801,610.5828055,612.0398715,613.497232,614.9528255,616.404296,617.84984705,619.2873877,620.71462085,622.1290727,623.5280044,624.908736,626.268617,627.60537975,628.9166977,630.20021485,631.4532807,632.6732742,633.85736815,635.00214635,636.1036036,637.15746965,638.159327,639.104552,639.9888155,640.807111,641.5541375,642.225772,642.8167135,643.3219555,643.736197,644.052959,644.266057,644.3681285,644.3503385,644.2023795,643.9124715,643.466773,642.8514425,642.050577,641.048568,639.8306905,638.3822195,636.690786,634.74461,632.5342675,630.0521015,627.291044,624.2460885,620.9134065,617.2891695,613.371905,609.160435,604.654465,599.8542895,594.760792,589.374267,583.696187,577.727141,571.4686015,564.9223355,558.088932,550.9686855,543.5618905,535.8670745,527.883354,519.609551,511.043604,502.183746,493.02821,483.576407,473.8286315,463.7866505,453.4531145,442.8333245,431.934643,420.7679665,409.339185,397.6659685,385.760097,373.6333505,361.306344,348.7938025,336.107506,323.2680695,310.296108,297.2063465,284.0194,270.7647185,257.459972,244.1346105,230.809249,217.5192275,204.285161,191.1394445,178.108583,165.2190815,152.50628,139.9937385,127.710907,115.6901805,103.958064,92.5440074999999,81.483351,70.8114345,60.5783230000001,50.8458615,41.681785,33.1685535,25.3945170000001,18.4480254999999,12.417429,7.39402250000001,3.463211,0.695674499999996,0.837906999999973,1.08157850000009,0,0,0,0,0,0,0,0,0,0,0];

x = 1:length(emg); %osa x

a_x = x(1); % pocatek segmentu
a_y = emg(a_x); % souradnice bodu A

[c_y, c_x] = findpeaks(emg, 'MinPeakHeight', (max(emg)-min(emg))/4); % lokalni maximum

b_x = 421; % maximalni plocha odpovídá b_x = 506
b_y = emg(b_x); % souradnice bodu B

trianglePlot(x, emg, S, a_x, a_y, b_x, b_y, c_x, c_y); %interaktivni zobrazeni trojuhelnikoveho detektoru

%
%% Funkce

function trianglePlot(x, emg, S, a_x, a_y, b_x, b_y, c_x, c_y)
    subplot(211)
    plot(x,emg, 'LineWidth', 2);
    hold on
    plot(a_x,a_y, 'rx', 'LineWidth', 2, 'MarkerSize',15)
    plot(b_x,b_y, 'rx', 'LineWidth', 2, 'MarkerSize',15)
    plot(c_x,c_y, 'rx', 'LineWidth', 2, 'MarkerSize',15)
    
    plot([a_x, c_x], [a_y, c_y], 'k', 'LineWidth', 2);
    plot([a_x, b_x], [a_y, b_y], 'k', 'LineWidth', 2);
    plot([b_x, c_x], [b_y, c_y], 'k', 'LineWidth', 2);
    
    text(a_x+5,a_y+.4,'A')
    text(b_x-5,b_y+.4,'B')
    text(c_x-35,c_y+.2,'C')
    hold off
    title('Trojúhelníkový detektor')
    ylabel('EMG [mV]')
    
    subplot(212);
    plot(x, S, 'k', 'LineWidth', 2)
    hold on
    plot(b_x,S(b_x), 'rx', 'LineWidth', 2, 'MarkerSize',15)
    plot(x(506),S(506), '.', 'LineWidth', 2, 'MarkerSize',15)
    text(x(506)-20,S(506)-80,'max')
    hold off
    title('Plocha trojúhelníku')
    xlabel('n [-]'),
    ylabel('S')
end