function [Y,Xf,Af] = rede_x2(X,~,~)
%MYNEURALNETWORKFUNCTION neural network simulation function.
%
% Generated by Neural Network Toolbox function genFunction, 01-Dec-2015 01:48:31.
% 
% [Y] = myNeuralNetworkFunction(X,~,~) takes these arguments:
% 
%   X = 1xTS cell, 1 inputs over TS timsteps
%   Each X{1,ts} = 12xQ matrix, input #1 at timestep ts.
% 
% and returns:
%   Y = 1xTS cell of 1 outputs over TS timesteps.
%   Each Y{1,ts} = 1xQ matrix, output #1 at timestep ts.
% 
% where Q is number of samples (or series) and TS is the number of timesteps.

%#ok<*RPMT0>

  % ===== NEURAL NETWORK CONSTANTS =====
  
  % Input 1
  x1_step1_xoffset = [2;0;0;0;0;0;0;0;0;0;0;0];
  x1_step1_gain = [0.00896860986547085;0.00892857142857143;0.00743494423791822;0.00355871886120996;0.00197433366238894;0.00186915887850467;0.00177304964539007;0.00170068027210884;0.00165152766308836;0.00169923534409516;0.000938526513374003;0.000860215053763441];
  x1_step1_ymin = -1;
  
  % Layer 1
  b1 = [1.8725288319553586;-1.8053255109261659;1.7116942020855797;1.5996848142496818;-1.5761090171319942;1.3913558255945513;1.2471361898349029;1.2160481384329678;1.1208608731511669;1.0308416822561934;0.81948522550820391;-0.790005014531311;-0.73840137482650803;0.58827728065572427;-0.53912562448594714;-0.41427280787012249;0.39840886976169387;0.27053475917436032;-0.088434851833010544;-0.047942868252573295;-0.08443043000745605;0.17602947638529914;-0.2239991014879506;-0.39541498296819322;0.47415772409994922;0.54112233360547168;0.64602518202652026;0.69050934199856107;0.93328264196796529;0.92462966352811915;-1.0294485261960888;-1.1765277095542845;1.2143554424753968;-1.3207817951994749;1.4139227007606534;-1.5063147610155074;-1.6001743001905659;-1.7505129996914022;1.8061715305356556;-1.9635167025291194];
  IW1_1 = [-0.3700952625235901 -0.38848263643247888 0.06466110073953335 -1.0854892643186247 0.70914744723301759 0.29264853196462581 -0.53874126477327478 0.38363901844351894 -0.26591863768002688 -0.85066519378044514 -0.50946041661332597 0.43484480422042049;0.81555999707448945 0.54154705840536466 -0.39934481279644973 0.63711003078168582 -0.37463841138112414 0.4454026017667494 0.55721521450395595 -0.67831315566109884 -0.049636738205089306 0.25745560070335982 0.17342259103410537 0.94563928091873106;-0.44158591725282181 -0.073826575248289242 0.36395616490499039 -0.78526721654084042 -0.7481944711166999 0.67686850254882325 0.78748346381794088 -0.66222206237693071 -0.192248531913849 0.47200959463055348 0.32395087456105631 -0.48000349627171829;-0.6028345153454131 0.41684659505035465 0.18973454771659212 0.87350900036566292 0.79231278141250505 0.18857752120623003 0.26649471409774983 -0.26164657629402366 -0.10275734113434881 -0.49520648848850046 0.67082109889119723 0.89480910855491624;0.31027192745638793 0.7371443045443351 -0.49883455061727788 0.53153011789113225 -0.77484412321724327 0.67727136882300609 0.0076988006901537905 -0.1392045103813434 -0.80752559911792454 -0.16524035219834204 -0.44897301351655655 -0.52161441854653734;-0.43340136307057292 0.52252922096130072 -0.79960154588530374 0.78604857838274922 -0.77256514217275774 0.28825393691317142 -0.13463117707963676 -0.7437076783298826 -0.15462951617152651 -0.26008904984548675 0.03365297007211452 0.79169546164619531;-0.54498289814485512 -0.85023063285083922 -0.91509797885588173 0.42795827380183121 0.12057267913999414 0.48825517976863503 -0.5371432451248862 0.31484668142989819 -0.67453534722137232 -0.018594967564915951 -0.065364947870179629 0.84331218876549296;-0.43210169760893746 0.96700971132156055 -0.62094784862761032 0.22292810268499361 -0.12371505740446084 0.078608646454263797 -0.10659268082910073 0.10255784554042317 -0.92315995561528308 -0.66648152774030966 -0.52899083530478508 -0.66110610853091001;-0.30641700193572813 0.61403090925717629 -0.012017590512753105 0.76980557285066142 -0.7441417351187084 -0.78139140897796877 -0.021869323924098107 0.7606837225150096 0.083394574605900357 0.37788367571159781 0.40003008632378501 -0.70688099758540412;-0.88143803142690302 0.26511738995595374 0.22130560675744129 0.11477004374148783 -0.81973454579863658 -0.54555140208808928 0.66128198162520724 -0.51647536322372478 0.20633844594811918 -0.7292268120958697 0.5266652037543228 -0.4241692580860611;-0.47795150007948972 -0.98353185001565768 -0.77958335526212841 -0.037915095664017753 -0.50511967307986494 -0.12662000855022557 0.40086588486848324 0.79213506787705612 0.44439923031528433 0.83380900438175232 -0.1262905352886935 0.18269171530682041;0.32488747260817319 0.96322190810151898 -0.66080710602988202 -0.46614607013303738 -0.73523752368544104 0.088696567836788143 0.20727794106356778 0.29161047840038523 -0.49581715861083187 -0.84626413566963787 -0.39422544055625985 0.48435607828228533;1.1163930501783417 -0.20289730195631905 -0.37205063745130412 -0.65393444926585931 0.27759238570354 -0.24465450428570798 -0.87295730631156743 0.53940250463499884 0.49850835137288901 0.61048410729638924 -0.044669021747770037 0.22205341614097202;-0.54823712199103636 -0.27566617915763991 -0.996109717817938 -0.032599897229462806 -0.056416603587112271 0.33629213237132344 -0.90662386254536098 -0.28875025479834221 0.4858291381497552 0.83210302379495127 -0.4758429522317652 -0.38849403176241853;0.3673287004687123 -0.0017514529504876137 -0.31999960219318924 0.65692958399545087 -1.2535941056290008 0.26611738305118526 0.2332588009775928 -0.086098885574456496 0.0085598702256409736 -0.13974044277939482 -1.0770617282258084 -0.20011120885806216;0.90387469904545059 0.13548842154416263 -0.37484703264100011 0.92318616337872017 -0.11818385661566037 0.51038869829031863 -0.049527234687385617 -0.71644058074189709 0.75394552941170057 0.19168729208843738 -0.21363447594207544 0.62202795539935296;-0.091358186538042965 0.87630413568576515 0.21681448908319298 -0.3718385293506416 0.82716421342746493 -0.67893168824327577 0.10646928654934371 0.82166455648439418 0.6145363303377912 -0.24759339661715313 -0.42619544398535614 0.28199552289120183;-0.38644504390210416 0.93239280586180751 -0.5167738487489667 -0.55706165780385763 0.20003459527778253 0.076384898300773121 -0.94278601922530181 -0.043754541195881985 0.059822831400517709 0.53047651611360758 -0.62753217113742565 -0.5646563348292637;0.29476385796851462 -0.18178072099663942 0.51789836152029367 -0.63248791115885283 0.8185398459219122 -0.48971426335494989 0.25130143046341552 0.59631346499454319 0.958397088274763 0.39220972278792976 0.55769938909060879 0.066853705575410483;0.074450487451045141 0.63147553252622224 -0.49835606187482751 -0.6686989814577341 0.79683817166321314 0.79154985588178084 0.21832301687559338 0.38137483866208521 0.28000935146628431 0.18058900012080592 0.75014011434518824 0.63727315488559877;-0.73993328309649575 -0.31360148053520098 -0.81936400402571896 0.2164495205625761 0.18852310598695413 0.80903861239198249 -0.014850139756353969 -0.35675883012108917 0.89506794287051161 0.68457714755100296 -0.48742905194929886 -0.50637331584651479;0.013232047849504748 -0.89973639944342465 0.70261559473274771 -0.24512512682223361 0.0035514360118163515 0.71905572042704635 -0.6101754063490944 0.18696986758559905 -0.67170080520514386 -0.026681645792629265 0.77439116084300474 0.60775316491325215;-0.80915325854070097 -0.48354061373141177 0.068730960434169763 0.80447858807765693 0.057627224114433009 -0.13056390765655929 0.53084291626957181 -0.28637508952387575 0.78891177020487147 0.83507539917383289 0.15634015116245306 -0.67194356530770127;-0.43549811994674625 -0.28901150893898808 -0.09304582028376418 -0.13833470173744203 0.257957280912957 0.60524692079079478 0.70928147025469634 0.69932739372705099 0.12840860377129543 -0.086308330612512349 -0.78611804948240427 -1.0234933300976017;-0.011633594022629262 -0.20249093997017459 -0.78416956863041387 0.61075501719490555 0.4055469007530903 0.66966985966216297 -0.40444826302276377 -0.77340020629985629 -0.67946171059727345 0.2998723783170214 0.40706105696344436 -0.76493742072033133;0.61154772545855818 -0.027464565795704464 0.1014605160413138 -0.54473650098477255 0.19676224786206198 -0.2502244012068649 0.014950839507052336 0.97111329294045456 0.92764532627964524 0.12542778838660493 0.29941493032449773 -0.96393751061548716;0.37732452429289443 0.34612629940499784 -0.73533292900241221 0.5255986658059506 0.39095908996578277 -0.71519299538201375 -0.49111677251210484 0.49611146891559499 0.61649192425950461 0.4268542578586354 -0.29669527307795535 0.86466332268131718;0.059958963407735669 -0.20632849893383956 -0.49060903986713256 0.73581785996467819 -0.11863829079773766 0.061713828237804115 -0.60405266172587002 -0.98193796766675223 -0.87913789011883758 0.072750246223562368 0.0413707624114521 0.78730279669514791;0.52591307577691937 0.58441589756543455 -0.6946942126953044 -0.17829097011598166 0.22014555563639129 -0.25435029738479631 0.53400131400926676 0.68284458461781683 -0.30235061271672131 0.58527127926119438 -0.73453907505547544 -0.69184068448532121;0.85065230220931354 -0.28168515684413714 -0.19985689943201257 0.22337987030818207 -0.54516585827397701 0.65660958804961256 -0.75737850140010443 0.67424639129145747 -0.15759288254898354 -0.18602970755636067 -0.92885593515808584 0.25075198940535964;-0.058031571472445677 0.44808097320371987 -0.27047904694208724 -0.73059333323461817 0.67655110329943124 -0.56110241100820946 -0.54257978212327074 0.35686776807369452 0.22911715991266293 0.73898383799881029 0.53588509728461431 0.85531940248598393;-0.34861506470527653 0.51614527852247338 0.55735865901330484 -0.3941868664217219 -0.3437709769773088 -0.40584380147308413 0.75829759718607703 0.62727129763661393 0.68528277044487806 -0.57253545730655653 -0.53641655295740409 -0.37064098959464276;0.8919314451156124 0.18356920211390104 0.81196860086814138 0.2000255565948037 -0.29256417967697623 0.066802192919722961 0.15849821862470931 -0.06949032647813122 -0.17534477744474297 1.029510669215782 0.38982360354677942 -0.86553918715164579;-0.0083107568834183895 0.93545048712711698 0.66687214308023757 0.046967428827701198 -0.45391464511586066 -0.96009369195196637 0.19418458768239286 -0.2953460039500948 0.49156770005968087 0.53004960465112827 0.64025188559054047 -0.32324557642514851;0.54204642735172348 -0.65891206458911211 -0.63103573032995108 0.59280449690462989 0.67287510306076448 -0.38315510584996754 0.64688862776187173 -0.61137107826985415 0.679322580029323 0.094084419668750571 0.43055981294661233 0.34498020322171147;-0.6027919296801233 0.15497371675517127 -0.13609281733039602 0.36167503826453384 -0.63961996761737727 0.28231029222483006 -0.84131061191257595 1.0368967290910229 -0.14404197642699654 -0.78879028845332588 0.40779656326027341 0.1828447284828654;-0.64752669018895948 -0.82716639666479663 -0.17473035955724978 -0.0045002693530891329 -0.66758881999486819 -0.64492547943109213 0.62117665219170759 0.60750856456431979 0.22220214325796786 0.55838290126640577 0.73377839460609107 -0.083070372757125421;-0.61994411701508578 -0.57289386548371624 -0.13114491835664385 0.26037852917386528 0.36727091642401954 0.21061623423485365 0.7093524756316687 -0.87005206998407136 0.7157779441090405 0.37281833528540914 -0.36138371146549592 0.72293465020912573;0.12383815690464148 -0.12738451634829567 0.75667532638986956 -0.64189564295240409 -0.63327680688106747 0.60314814807555683 -0.45767701694204643 0.27048237716853613 0.56851919636296355 -0.15325777963950132 0.76073282057685232 -0.79534095465430665;-0.0047521870851592804 -0.51716560870306705 -0.52065623094439528 -0.96866193012021706 -0.63416038387307871 -0.016752605939620354 -0.73924771409248058 0.31988683208540436 -0.80811772166577323 -0.54227806470546969 0.1082530926646154 0.010126571280138146];
  
  % Layer 2
  b2 = 0.029763818808293693;
  LW2_1 = [-0.62742248523468258 -0.0064713762283895096 0.12899847421079666 0.24134210510378626 -0.28488999738529386 -0.3079474401399882 -0.55286672419365379 0.23915779166486309 0.18057163644815116 0.34417599342568334 -0.86356305429568236 0.39306177711457291 0.47978274233973495 -0.37617019297618526 0.039164124373851139 0.3729702153615464 -0.21787066704759217 0.72959637860572124 -0.53801352023284121 0.079986861494694819 -1.0499175771259812 -0.52808145933791439 -0.50016654251918569 0.66294651064985111 -0.56856039716126228 -0.0063088339622296846 -0.21660556770732448 0.37919435683587838 -0.66729610475011181 0.52001271780709968 -0.0071769164003693741 -0.46112624750586895 0.14250956919743563 0.053392228741766946 -0.23300196093423559 0.26429680019857549 -0.24124860223996247 0.32902042047893765 0.80203696598553198 0.51973296700411498];
  
  % ===== SIMULATION ========
  
  % Format Input Arguments
  isCellX = iscell(X);
  if ~isCellX, X = {X}; end;
  
  % Dimensions
  TS = size(X,2); % timesteps
  if ~isempty(X)
    Q = size(X{1},2); % samples/series
  else
    Q = 0;
  end
  
  % Allocate Outputs
  Y = cell(1,TS);
  
  % Time loop
  for ts=1:TS
  
    % Input 1
    Xp1 = mapminmax_apply(X{1,ts},x1_step1_gain,x1_step1_xoffset,x1_step1_ymin);
    
    % Layer 1
    a1 = tansig_apply(repmat(b1,1,Q) + IW1_1*Xp1);
    
    % Layer 2
    a2 = logsig_apply(repmat(b2,1,Q) + LW2_1*a1);
    
    % Output 1
    Y{1,ts} = a2;
  end
  
  % Final Delay States
  Xf = cell(1,0);
  Af = cell(2,0);
  
  % Format Output Arguments
  if ~isCellX, Y = cell2mat(Y); end
end

% ===== MODULE FUNCTIONS ========

% Map Minimum and Maximum Input Processing Function
function y = mapminmax_apply(x,settings_gain,settings_xoffset,settings_ymin)
  y = bsxfun(@minus,x,settings_xoffset);
  y = bsxfun(@times,y,settings_gain);
  y = bsxfun(@plus,y,settings_ymin);
end

% Sigmoid Positive Transfer Function
function a = logsig_apply(n)
  a = 1 ./ (1 + exp(-n));
end

% Sigmoid Symmetric Transfer Function
function a = tansig_apply(n)
  a = 2 ./ (1 + exp(-2*n)) - 1;
end
