csky_math.h 141 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783
  1. /******************************************************************************
  2. * @file csky_math.h
  3. * @brief Public header file for CSI DSP Library.
  4. * @version V1.0
  5. * @date 20. Dec 2016
  6. ******************************************************************************/
  7. /* ---------------------------------------------------------------------------
  8. * Copyright (C) 2016 CSKY Limited. All rights reserved.
  9. *
  10. * Redistribution and use of this software in source and binary forms,
  11. * with or without modification, are permitted provided that the following
  12. * conditions are met:
  13. * * Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * * Neither the name of CSKY Ltd. nor the names of CSKY's contributors may
  19. * be used to endorse or promote products derived from this software without
  20. * specific prior written permission of CSKY Ltd.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  26. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  27. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. * -------------------------------------------------------------------------- */
  34. /**
  35. * @defgroup groupMath Basic Math Functions
  36. */
  37. /**
  38. * @defgroup groupFastMath Fast Math Functions
  39. * This set of functions provides a fast approximation to sine, cosine, and square root.
  40. * As compared to most of the other functions in the CSI math library, the fast math functions
  41. * operate on individual values and not arrays.
  42. * There are separate functions for Q15, Q31, and floating-point data.
  43. *
  44. */
  45. /**
  46. * @defgroup groupCmplxMath Complex Math Functions
  47. * This set of functions operates on complex data vectors.
  48. * The data in the complex arrays is stored in an interleaved fashion
  49. * (real, imag, real, imag, ...).
  50. * In the API functions, the number of samples in a complex array refers
  51. * to the number of complex values; the array contains twice this number of
  52. * real values.
  53. */
  54. /**
  55. * @defgroup groupFilters Filtering Functions
  56. */
  57. /**
  58. * @defgroup groupMatrix Matrix Functions
  59. *
  60. * This set of functions provides basic matrix math operations.
  61. * The functions operate on matrix data structures. For example,
  62. * the type
  63. * definition for the floating-point matrix structure is shown
  64. * below:
  65. * <pre>
  66. * typedef struct
  67. * {
  68. * uint16_t numRows; // number of rows of the matrix.
  69. * uint16_t numCols; // number of columns of the matrix.
  70. * float32_t *pData; // points to the data of the matrix.
  71. * } csky_matrix_instance_f32;
  72. * </pre>
  73. * There are similar definitions for Q15 and Q31 data types.
  74. *
  75. * The structure specifies the size of the matrix and then points to
  76. * an array of data. The array is of size <code>numRows X numCols</code>
  77. * and the values are arranged in row order. That is, the
  78. * matrix element (i, j) is stored at:
  79. * <pre>
  80. * pData[i*numCols + j]
  81. * </pre>
  82. *
  83. * \par Init Functions
  84. * There is an associated initialization function for each type of matrix
  85. * data structure.
  86. * The initialization function sets the values of the internal structure fields.
  87. * Refer to the function <code>csky_mat_init_f32()</code>, <code>csky_mat_init_q31()</code>
  88. * and <code>csky_mat_init_q15()</code> for floating-point, Q31 and Q15 types, respectively.
  89. *
  90. * \par
  91. * Use of the initialization function is optional. However, if initialization function is used
  92. * then the instance structure cannot be placed into a const data section.
  93. * To place the instance structure in a const data
  94. * section, manually initialize the data structure. For example:
  95. * <pre>
  96. * <code>csky_matrix_instance_f32 S = {nRows, nColumns, pData};</code>
  97. * <code>csky_matrix_instance_q31 S = {nRows, nColumns, pData};</code>
  98. * <code>csky_matrix_instance_q15 S = {nRows, nColumns, pData};</code>
  99. * </pre>
  100. * where <code>nRows</code> specifies the number of rows, <code>nColumns</code>
  101. * specifies the number of columns, and <code>pData</code> points to the
  102. * data array.
  103. *
  104. * \par Size Checking
  105. * By default all of the matrix functions perform size checking on the input and
  106. * output matrices. For example, the matrix addition function verifies that the
  107. * two input matrices and the output matrix all have the same number of rows and
  108. * columns. If the size check fails the functions return:
  109. * <pre>
  110. * CSKY_MATH_SIZE_MISMATCH
  111. * </pre>
  112. * Otherwise the functions return
  113. * <pre>
  114. * CSKY_MATH_SUCCESS
  115. * </pre>
  116. * There is some overhead associated with this matrix size checking.
  117. * The matrix size checking is enabled via the \#define
  118. * <pre>
  119. * CSKY_MATH_MATRIX_CHECK
  120. * </pre>
  121. * within the library project settings. By default this macro is defined
  122. * and size checking is enabled. By changing the project settings and
  123. * undefining this macro size checking is eliminated and the functions
  124. * run a bit faster. With size checking disabled the functions always
  125. * return <code>CSKY_MATH_SUCCESS</code>.
  126. */
  127. /**
  128. * @defgroup groupTransforms Transform Functions
  129. */
  130. /**
  131. * @defgroup groupController Controller Functions
  132. */
  133. /**
  134. * @defgroup groupStats Statistics Functions
  135. */
  136. /**
  137. * @defgroup groupSupport Support Functions
  138. */
  139. /**
  140. * @defgroup groupInterpolation Interpolation Functions
  141. * These functions perform 1- and 2-dimensional interpolation of data.
  142. * Linear interpolation is used for 1-dimensional data and
  143. * bilinear interpolation is used for 2-dimensional data.
  144. */
  145. /**
  146. * @defgroup groupYunvoice Yunvoice Functions
  147. * These functions are designed for Yunvoice project, which are modified
  148. * according to the CEVA DSP functions. So, one can porting the software
  149. * from CEVA to CSKY straightforwardly.
  150. */
  151. /**
  152. * @defgroup groupExamples Examples
  153. */
  154. #ifndef _CSKY_MATH_H
  155. #define _CSKY_MATH_H
  156. #define __CSI_GENERIC /* disable NVIC and Systick functions */
  157. #include "csi_core.h"
  158. #include <float.h>
  159. #undef __CSI_GENERIC /* enable NVIC and Systick functions */
  160. #include "string.h"
  161. #include "math.h"
  162. #ifdef __cplusplus
  163. extern "C"
  164. {
  165. #endif
  166. /**
  167. * @brief Macros required for reciprocal calculation in Normalized LMS
  168. */
  169. #define DELTA_Q31 (0x100)
  170. #define DELTA_Q15 0x5
  171. #define INDEX_MASK 0x0000003F
  172. #ifndef PI
  173. #define PI 3.14159265358979f
  174. #endif
  175. /**
  176. * @brief Macros required for SINE and COSINE Fast math approximations
  177. */
  178. #define FAST_MATH_TABLE_SIZE 512
  179. #define FAST_MATH_Q31_SHIFT (32 - 10)
  180. #define FAST_MATH_Q15_SHIFT (16 - 10)
  181. #define CONTROLLER_Q31_SHIFT (32 - 9)
  182. #define TABLE_SIZE 256
  183. #define TABLE_SPACING_Q31 0x400000
  184. #define TABLE_SPACING_Q15 0x80
  185. /**
  186. * @brief Macros required for SINE and COSINE Controller functions
  187. */
  188. /* 1.31(q31) Fixed value of 2/360 */
  189. /* -1 to +1 is divided into 360 values so total spacing is (2/360) */
  190. #define INPUT_SPACING 0xB60B61
  191. /**
  192. * @brief Macro for Unaligned Support
  193. */
  194. #ifndef UNALIGNED_SUPPORT_DISABLE
  195. #define ALIGN4
  196. #else
  197. #define ALIGN4 __attribute__((aligned(4)))
  198. #endif /* #ifndef UNALIGNED_SUPPORT_DISABLE */
  199. /**
  200. * @brief Macro for log , pow and related fast functions.
  201. */
  202. #define ABS(x) (((x) > 0) ? (x) : (-x))
  203. #define max(x) (((y) > (x)) ? (y) : (x))
  204. #define min(x) (((y) < (x)) ? (y) : (x))
  205. #define CN 124217729.0
  206. #define HIGH_HALF 1
  207. #define LOW_HALF 0
  208. /* Exact addition of two single-length floating point numbers. */
  209. /* The macro produces a double-length number (z,zz) that satisfies */
  210. /* z+zz = x+y exactly. */
  211. #define EADD(x,y,z,zz) \
  212. z=(x)+(y); zz=(ABS(x)>ABS(y)) ? (((x)-(z))+(y)) : (((y)-(z))+(x));
  213. /* Exact multiplication of two single-length floating point numbers, */
  214. /*The macro produces a double-length number (z,zz) that */
  215. /* satisfies z+zz = x*y exactly. p,hx,tx,hy,ty are temporary */
  216. /* storage variables of type double. */
  217. # define EMULV(x,y,z,zz,p,hx,tx,hy,ty) \
  218. p=CN*(x); hx=((x)-p)+p; tx=(x)-hx; \
  219. p=CN*(y); hy=((y)-p)+p; ty=(y)-hy; \
  220. z=(x)*(y); zz=(((hx*hy-z)+hx*ty)+tx*hy)+tx*ty;
  221. /* Exact multiplication of two single-length floating point numbers. */
  222. /* The macro produces a nearly double-length number (z,zz) (see Dekker) */
  223. /* that satisfies z+zz = x*y exactly. p,hx,tx,hy,ty,q are temporary */
  224. /* storage variables of type double. */
  225. # define MUL12(x,y,z,zz,p,hx,tx,hy,ty,q) \
  226. p=CN*(x); hx=((x)-p)+p; tx=(x)-hx; \
  227. p=CN*(y); hy=((y)-p)+p; ty=(y)-hy; \
  228. p=hx*hy; q=hx*ty+tx*hy; z=p+q; zz=((p-z)+q)+tx*ty;
  229. /* Double-length addition, Dekker. The macro produces a double-length */
  230. /* number (z,zz) which satisfies approximately z+zz = x+xx + y+yy. */
  231. /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */
  232. /* are assumed to be double-length numbers. r,s are temporary */
  233. /* storage variables of type double. */
  234. #define ADD2(x,xx,y,yy,z,zz,r,s) \
  235. r=(x)+(y); s=(ABS(x)>ABS(y)) ? \
  236. (((((x)-r)+(y))+(yy))+(xx)) : \
  237. (((((y)-r)+(x))+(xx))+(yy)); \
  238. z=r+s; zz=(r-z)+s;
  239. /* Double-length subtraction, Dekker. The macro produces a double-length */
  240. /* number (z,zz) which satisfies approximately z+zz = x+xx - (y+yy). */
  241. /* An error bound: (abs(x+xx)+abs(y+yy))*4.94e-32. (x,xx), (y,yy) */
  242. /* are assumed to be double-length numbers. r,s are temporary */
  243. /* storage variables of type double. */
  244. #define SUB2(x,xx,y,yy,z,zz,r,s) \
  245. r=(x)-(y); s=(ABS(x)>ABS(y)) ? \
  246. (((((x)-r)-(y))-(yy))+(xx)) : \
  247. ((((x)-((y)+r))+(xx))-(yy)); \
  248. z=r+s; zz=(r-z)+s;
  249. /* Double-length multiplication, Dekker. The macro produces a double-length */
  250. /* number (z,zz) which satisfies approximately z+zz = (x+xx)*(y+yy). */
  251. /* An error bound: abs((x+xx)*(y+yy))*1.24e-31. (x,xx), (y,yy) */
  252. /* are assumed to be double-length numbers. p,hx,tx,hy,ty,q,c,cc are */
  253. /* temporary storage variables of type double. */
  254. #define MUL2(x,xx,y,yy,z,zz,p,hx,tx,hy,ty,q,c,cc) \
  255. MUL12(x,y,c,cc,p,hx,tx,hy,ty,q) \
  256. cc=((x)*(yy)+(xx)*(y))+cc; z=c+cc; zz=(c-z)+cc;
  257. __STATIC_INLINE int32_t __SSAT_31(int32_t x)
  258. {
  259. int32_t res = x;
  260. if (x > 0x3fffffff) {
  261. res = 0x3fffffff;
  262. } else if (x < -1073741824) {
  263. res = -1073741824;
  264. }
  265. return res;
  266. }
  267. __STATIC_INLINE int32_t __SSAT_16(int32_t x)
  268. {
  269. int32_t res = x;
  270. if (x > 0x7fff) {
  271. res = 0x7fff;
  272. } else if (x < -32768) {
  273. res = -32768;
  274. }
  275. return res;
  276. }
  277. __STATIC_INLINE int32_t __SSAT_8(int32_t x)
  278. {
  279. int32_t res = x;
  280. if (x > 0x7f) {
  281. res = 0x7f;
  282. } else if (x < -128) {
  283. res = -128;
  284. }
  285. return res;
  286. }
  287. #ifdef CSKY_SIMD
  288. /* SMMLAR */
  289. __STATIC_INLINE int32_t multAcc_32x32_keep32_R(int32_t a, int32_t x, int32_t y)
  290. {
  291. __ASM volatile("mula.s32.rhs %0, %1, %2\n\t"
  292. :"=r" (a), "=r" (x), "=r" (y) : "0" (a), "1" (x), "2" (y));
  293. return a;
  294. }
  295. /* SMMLSR */
  296. __STATIC_INLINE int32_t multSub_32x32_keep32_R(int32_t a, int32_t x, int32_t y)
  297. {
  298. __ASM volatile("muls.s32.rhs %0, %1, %2\n\t"
  299. :"=r" (a), "=r" (x), "=r" (y): "0" (a), "1" (x), "2" (y));
  300. return a;
  301. }
  302. /* SMMULR */
  303. __STATIC_INLINE int32_t mult_32x32_keep32_R(int32_t x, int32_t y)
  304. {
  305. int32_t a;
  306. __ASM volatile("mul.s32.rh %0, %1, %2\n\t"
  307. :"=r" (a), "=r" (x), "=r" (y): "1" (x), "2" (y));
  308. return a;
  309. }
  310. /* SMMLA */
  311. __STATIC_INLINE int32_t multAcc_32x32_keep32(int32_t a, int32_t x, int32_t y)
  312. {
  313. __ASM volatile("mula.s32.hs %0, %1, %2\n\t"
  314. :"=r" (a), "=r" (x), "=r" (y): "0" (a), "1" (x), "2" (y));
  315. return a;
  316. }
  317. /* SMMLS */
  318. __STATIC_INLINE int32_t multSub_32x32_keep32(int32_t a, int32_t x, int32_t y)
  319. {
  320. __ASM volatile("muls.s32.hs %0, %1, %2\n\t"
  321. :"=r" (a), "=r" (x), "=r" (y): "0" (a), "1" (x), "2" (y));
  322. return a;
  323. }
  324. /* SMMUL */
  325. __STATIC_INLINE int32_t mult_32x32_keep32(int32_t x, int32_t y)
  326. {
  327. int32_t a;
  328. __ASM volatile("mul.s32.h %0, %1, %2\n\t"
  329. :"=r" (a), "=r" (x), "=r" (y): "0" (a), "1" (x), "2" (y));
  330. return a;
  331. }
  332. __STATIC_INLINE int32_t multAcc_16x16_keep32(int32_t a, int16_t x, int16_t y)
  333. {
  334. __ASM volatile("mulall.s16 %0, %1, %2\n\t"
  335. :"=r" (a), "=r" (x), "=r" (y): "0" (a), "1" (x), "2" (y));
  336. return a;
  337. }
  338. __STATIC_INLINE int64_t multAcc_16x16_keep64(int64_t a, int16_t x, int16_t y)
  339. {
  340. __ASM volatile("mulall.s16.e %0, %1, %2\n\t"
  341. :"=r" (a), "=r" (x), "=r" (y): "0" (a), "1" (x), "2" (y));
  342. return a;
  343. }
  344. __STATIC_INLINE int64_t mult_32x32_keep64(int32_t x, int32_t y)
  345. {
  346. int64_t a;
  347. __ASM volatile("mul.s32 %0, %1, %2\n\t"
  348. :"=r" (a), "=r" (x), "=r" (y): "1" (x), "2" (y));
  349. return a;
  350. }
  351. __STATIC_INLINE int64_t multAcc_32x32_keep64(int64_t a, int32_t x, int32_t y)
  352. {
  353. __ASM volatile("mula.s32 %0, %1, %2\n\t"
  354. :"=r" (a), "=r" (x), "=r" (y): "0" (a), "1" (x), "2" (y));
  355. return a;
  356. }
  357. __STATIC_INLINE int32_t mult_32x32_dext_31(int32_t x, int32_t y)
  358. {
  359. int64_t tmp1;
  360. int32_t tmp2;
  361. __ASM volatile("mul.s32 %0, %1, %2\n\t"
  362. "dexti %3, %0, %R0, 31"
  363. :"=r" (tmp1), "=r" (x), "=r" (y), "=r" (tmp2): "1" (x), "2" (y));
  364. return tmp2;
  365. }
  366. __STATIC_INLINE int32_t mult_32x32_dext_30(int32_t x, int32_t y)
  367. {
  368. int64_t tmp1;
  369. int32_t tmp2;
  370. __ASM volatile("mul.s32 %0, %1, %2\n\t"
  371. "dexti %3, %0, %R0, 30"
  372. :"=r" (tmp1), "=r" (x), "=r" (y), "=r" (tmp2): "1" (x), "2" (y));
  373. return tmp2;
  374. }
  375. __STATIC_INLINE int32_t mult_32x32_dext_4(int32_t x, int32_t y)
  376. {
  377. int64_t tmp1;
  378. int32_t tmp2;
  379. __ASM volatile("mul.s32 %0, %1, %2\n\t"
  380. "dexti %3, %0, %R0, 4"
  381. :"=r" (tmp1), "=r" (x), "=r" (y), "=r" (tmp2): "1" (x), "2" (y));
  382. return tmp2;
  383. }
  384. __STATIC_INLINE int32_t mult_32x32_dext_33(int32_t x, int32_t y)
  385. {
  386. int64_t tmp1;
  387. int32_t tmp2;
  388. __ASM volatile("mul.s32 %0, %1, %2\n\t"
  389. "asri %3, %R0, 1"
  390. :"=r" (tmp1), "=r" (x), "=r" (y), "=r" (tmp2): "1" (x), "2" (y));
  391. return tmp2;
  392. }
  393. __STATIC_INLINE int32_t dext_31(int64_t x)
  394. {
  395. int32_t tmp1;
  396. __ASM volatile(
  397. "dexti %0, %1, %R1, 31"
  398. :"=r" (tmp1), "=r" (x) : "1" (x));
  399. return tmp1;
  400. }
  401. __STATIC_INLINE int32_t mult_l16xl16_keep32(int32_t x, int32_t y)
  402. {
  403. int32_t a;
  404. __ASM volatile("mulll.s16 %0, %1, %2\n\t"
  405. :"=r" (a), "=r" (x), "=r" (y): "1" (x), "2" (y));
  406. return a;
  407. }
  408. __STATIC_INLINE int32_t mult_h16xl16_keep32(int32_t x, int32_t y)
  409. {
  410. int32_t a;
  411. __ASM volatile("mulhl.s16 %0, %1, %2\n\t"
  412. :"=r" (a), "=r" (x), "=r" (y): "1" (x), "2" (y));
  413. return a;
  414. }
  415. __STATIC_INLINE int32_t mult_h16xh16_keep32(int32_t x, int32_t y)
  416. {
  417. int32_t a;
  418. __ASM volatile("mulhh.s16 %0, %1, %2\n\t"
  419. :"=r" (a), "=r" (x), "=r" (y): "1" (x), "2" (y));
  420. return a;
  421. }
  422. #endif
  423. /**
  424. * @brief Error status returned by some functions in the library.
  425. */
  426. typedef enum
  427. {
  428. CSKY_MATH_SUCCESS = 0, /**< No error */
  429. CSKY_MATH_ARGUMENT_ERROR = -1, /**< One or more arguments are incorrect */
  430. CSKY_MATH_LENGTH_ERROR = -2, /**< Length of data buffer is incorrect */
  431. CSKY_MATH_SIZE_MISMATCH = -3, /**< Size of matrices is not compatible with the operation. */
  432. CSKY_MATH_NANINF = -4, /**< Not-a-number (NaN) or infinity is generated */
  433. CSKY_MATH_SINGULAR = -5, /**< Generated by matrix inversion if the input matrix is singular and cannot be inverted. */
  434. CSKY_MATH_TEST_FAILURE = -6 /**< Test Failed */
  435. } csky_status;
  436. /**
  437. * @brief 8-bit fractional data type in 1.7 format.
  438. */
  439. typedef int8_t q7_t;
  440. /**
  441. * @brief 16-bit fractional data type in 1.15 format.
  442. */
  443. typedef int16_t q15_t;
  444. /**
  445. * @brief 32-bit fractional data type in 1.31 format.
  446. */
  447. typedef int32_t q31_t;
  448. /**
  449. * @brief 64-bit fractional data type in 1.63 format.
  450. */
  451. typedef int64_t q63_t;
  452. /**
  453. * @brief 32-bit floating-point type definition.
  454. */
  455. typedef float float32_t;
  456. /**
  457. * @brief 64-bit floating-point type definition.
  458. */
  459. typedef double float64_t;
  460. /**
  461. * @brief 32-bit fractional complex data type in 1.31 format.
  462. */
  463. typedef struct
  464. {
  465. q31_t re;
  466. q31_t im;
  467. } cq31_t;
  468. /**
  469. * @brief 16-bit fractional complex data type in 1.15 format.
  470. */
  471. typedef struct
  472. {
  473. q15_t re;
  474. q15_t im;
  475. } cq15_t;
  476. /**
  477. * @brief definition to read/write two 16 bit values.
  478. */
  479. #define __SIMD32_TYPE int32_t
  480. #define CSI_UNUSED __attribute__((unused))
  481. #define __SIMD32(addr) (*(__SIMD32_TYPE **) & (addr))
  482. #define __SIMD32_CONST(addr) ((__SIMD32_TYPE *)(addr))
  483. #define _SIMD32_OFFSET(addr) (*(__SIMD32_TYPE *) (addr))
  484. #define __SIMD64(addr) (*(int64_t **) & (addr))
  485. #if defined (CSKY_MATH_NO_SIMD)
  486. /**
  487. * @brief definition to pack two 16 bit values.
  488. */
  489. #define __PKHBT(ARG1, ARG2, ARG3) ( (((int32_t)(ARG1) << 0) & (int32_t)0x0000FFFF) | \
  490. (((int32_t)(ARG2) << ARG3) & (int32_t)0xFFFF0000) )
  491. #define __PKHTB(ARG1, ARG2, ARG3) ( (((int32_t)(ARG1) << 0) & (int32_t)0xFFFF0000) | \
  492. (((int32_t)(ARG2) >> ARG3) & (int32_t)0x0000FFFF) )
  493. #endif
  494. /**
  495. * @brief definition to pack four 8 bit values.
  496. */
  497. #ifndef CSKY_MATH_BIG_ENDIAN
  498. #define __PACKq7(v0,v1,v2,v3) ( (((int32_t)(v0) << 0) & (int32_t)0x000000FF) | \
  499. (((int32_t)(v1) << 8) & (int32_t)0x0000FF00) | \
  500. (((int32_t)(v2) << 16) & (int32_t)0x00FF0000) | \
  501. (((int32_t)(v3) << 24) & (int32_t)0xFF000000) )
  502. #else
  503. #define __PACKq7(v0,v1,v2,v3) ( (((int32_t)(v3) << 0) & (int32_t)0x000000FF) | \
  504. (((int32_t)(v2) << 8) & (int32_t)0x0000FF00) | \
  505. (((int32_t)(v1) << 16) & (int32_t)0x00FF0000) | \
  506. (((int32_t)(v0) << 24) & (int32_t)0xFF000000) )
  507. #endif
  508. /**
  509. * @brief Clips Q63 to Q31 values.
  510. */
  511. static __INLINE q31_t clip_q63_to_q31(
  512. q63_t x)
  513. {
  514. return ((q31_t) (x >> 32) != ((q31_t) x >> 31)) ?
  515. ((0x7FFFFFFF ^ ((q31_t) (x >> 63)))) : (q31_t) x;
  516. }
  517. /**
  518. * @brief Instance structure for the Q7 FIR filter.
  519. */
  520. typedef struct
  521. {
  522. uint16_t numTaps; /**< number of filter coefficients in the filter. */
  523. q7_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  524. q7_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps.*/
  525. } csky_fir_instance_q7;
  526. /**
  527. * @brief Instance structure for the Q15 FIR filter.
  528. */
  529. typedef struct
  530. {
  531. uint16_t numTaps; /**< number of filter coefficients in the filter. */
  532. q15_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  533. q15_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps.*/
  534. } csky_fir_instance_q15;
  535. /**
  536. * @brief Instance structure for the Q31 FIR filter.
  537. */
  538. typedef struct
  539. {
  540. uint16_t numTaps; /**< number of filter coefficients in the filter. */
  541. q31_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  542. q31_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps. */
  543. } csky_fir_instance_q31;
  544. /**
  545. * @brief Instance structure for the floating-point FIR filter.
  546. */
  547. typedef struct
  548. {
  549. uint16_t numTaps; /**< number of filter coefficients in the filter. */
  550. float32_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  551. float32_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps. */
  552. } csky_fir_instance_f32;
  553. void csky_fir_q7(
  554. const csky_fir_instance_q7 * S,
  555. q7_t * pSrc,
  556. q7_t * pDst,
  557. uint32_t blockSize);
  558. void csky_fir_init_q7(
  559. csky_fir_instance_q7 * S,
  560. uint16_t numTaps,
  561. q7_t * pCoeffs,
  562. q7_t * pState,
  563. uint32_t blockSize);
  564. void csky_fir_q15(
  565. const csky_fir_instance_q15 * S,
  566. q15_t * pSrc,
  567. q15_t * pDst,
  568. uint32_t blockSize);
  569. void csky_fir_fast_q15(
  570. const csky_fir_instance_q15 * S,
  571. q15_t * pSrc,
  572. q15_t * pDst,
  573. uint32_t blockSize);
  574. csky_status csky_fir_init_q15(
  575. csky_fir_instance_q15 * S,
  576. uint16_t numTaps,
  577. q15_t * pCoeffs,
  578. q15_t * pState,
  579. uint32_t blockSize);
  580. void csky_fir_q31(
  581. const csky_fir_instance_q31 * S,
  582. q31_t * pSrc,
  583. q31_t * pDst,
  584. uint32_t blockSize);
  585. void csky_fir_fast_q31(
  586. const csky_fir_instance_q31 * S,
  587. q31_t * pSrc,
  588. q31_t * pDst,
  589. uint32_t blockSize);
  590. void csky_fir_init_q31(
  591. csky_fir_instance_q31 * S,
  592. uint16_t numTaps,
  593. q31_t * pCoeffs,
  594. q31_t * pState,
  595. uint32_t blockSize);
  596. void csky_fir_f32(
  597. const csky_fir_instance_f32 * S,
  598. float32_t * pSrc,
  599. float32_t * pDst,
  600. uint32_t blockSize);
  601. void csky_fir_init_f32(
  602. csky_fir_instance_f32 * S,
  603. uint16_t numTaps,
  604. float32_t * pCoeffs,
  605. float32_t * pState,
  606. uint32_t blockSize);
  607. /**
  608. * @brief Instance structure for the Q15 Biquad cascade filter.
  609. */
  610. typedef struct
  611. {
  612. int8_t numStages; /**< number of 2nd order stages in the filter. Overall order is 2*numStages. */
  613. q15_t *pState; /**< Points to the array of state coefficients. The array is of length 4*numStages. */
  614. q15_t *pCoeffs; /**< Points to the array of coefficients. The array is of length 5*numStages. */
  615. int8_t postShift; /**< Additional shift, in bits, applied to each output sample. */
  616. } csky_biquad_casd_df1_inst_q15;
  617. /**
  618. * @brief Instance structure for the Q31 Biquad cascade filter.
  619. */
  620. typedef struct
  621. {
  622. uint32_t numStages; /**< number of 2nd order stages in the filter. Overall order is 2*numStages. */
  623. q31_t *pState; /**< Points to the array of state coefficients. The array is of length 4*numStages. */
  624. q31_t *pCoeffs; /**< Points to the array of coefficients. The array is of length 5*numStages. */
  625. uint8_t postShift; /**< Additional shift, in bits, applied to each output sample. */
  626. } csky_biquad_casd_df1_inst_q31;
  627. /**
  628. * @brief Instance structure for the Q31 Biquad cascade filter.
  629. */
  630. /**
  631. * @brief Instance structure for the floating-point Biquad cascade filter.
  632. */
  633. typedef struct
  634. {
  635. uint32_t numStages; /**< number of 2nd order stages in the filter. Overall order is 2*numStages. */
  636. float32_t *pState; /**< Points to the array of state coefficients. The array is of length 4*numStages. */
  637. float32_t *pCoeffs; /**< Points to the array of coefficients. The array is of length 5*numStages. */
  638. } csky_biquad_casd_df1_inst_f32;
  639. void csky_biquad_cascade_df1_q15(
  640. const csky_biquad_casd_df1_inst_q15 * S,
  641. q15_t * pSrc,
  642. q15_t * pDst,
  643. uint32_t blockSize);
  644. void csky_biquad_cascade_df1_init_q15(
  645. csky_biquad_casd_df1_inst_q15 * S,
  646. uint8_t numStages,
  647. q15_t * pCoeffs,
  648. q15_t * pState,
  649. int8_t postShift);
  650. void csky_biquad_cascade_df1_fast_q15(
  651. const csky_biquad_casd_df1_inst_q15 * S,
  652. q15_t * pSrc,
  653. q15_t * pDst,
  654. uint32_t blockSize);
  655. void csky_biquad_cascade_df1_q31(
  656. const csky_biquad_casd_df1_inst_q31 * S,
  657. q31_t * pSrc,
  658. q31_t * pDst,
  659. uint32_t blockSize);
  660. void csky_biquad_cascade_df1_fast_q31(
  661. const csky_biquad_casd_df1_inst_q31 * S,
  662. q31_t * pSrc,
  663. q31_t * pDst,
  664. uint32_t blockSize);
  665. void csky_biquad_cascade_df1_init_q31(
  666. csky_biquad_casd_df1_inst_q31 * S,
  667. uint8_t numStages,
  668. q31_t * pCoeffs,
  669. q31_t * pState,
  670. int8_t postShift);
  671. void csky_biquad_cascade_df1_f32(
  672. const csky_biquad_casd_df1_inst_f32 * S,
  673. float32_t * pSrc,
  674. float32_t * pDst,
  675. uint32_t blockSize);
  676. void csky_biquad_cascade_df1_init_f32(
  677. csky_biquad_casd_df1_inst_f32 * S,
  678. uint8_t numStages,
  679. float32_t * pCoeffs,
  680. float32_t * pState);
  681. /**
  682. * @brief Instance structure for the floating-point matrix structure.
  683. */
  684. typedef struct
  685. {
  686. uint16_t numRows; /**< number of rows of the matrix. */
  687. uint16_t numCols; /**< number of columns of the matrix. */
  688. float32_t *pData; /**< points to the data of the matrix. */
  689. } csky_matrix_instance_f32;
  690. /**
  691. * @brief Instance structure for the floating-point matrix structure.
  692. */
  693. typedef struct
  694. {
  695. uint16_t numRows; /**< number of rows of the matrix. */
  696. uint16_t numCols; /**< number of columns of the matrix. */
  697. float64_t *pData; /**< points to the data of the matrix. */
  698. } csky_matrix_instance_f64;
  699. /**
  700. * @brief Instance structure for the Q15 matrix structure.
  701. */
  702. typedef struct
  703. {
  704. uint16_t numRows; /**< number of rows of the matrix. */
  705. uint16_t numCols; /**< number of columns of the matrix. */
  706. q15_t *pData; /**< points to the data of the matrix. */
  707. } csky_matrix_instance_q15;
  708. /**
  709. * @brief Instance structure for the Q31 matrix structure.
  710. */
  711. typedef struct
  712. {
  713. uint16_t numRows; /**< number of rows of the matrix. */
  714. uint16_t numCols; /**< number of columns of the matrix. */
  715. q31_t *pData; /**< points to the data of the matrix. */
  716. } csky_matrix_instance_q31;
  717. csky_status csky_mat_add_f32(
  718. const csky_matrix_instance_f32 * pSrcA,
  719. const csky_matrix_instance_f32 * pSrcB,
  720. csky_matrix_instance_f32 * pDst);
  721. csky_status csky_mat_add_q15(
  722. const csky_matrix_instance_q15 * pSrcA,
  723. const csky_matrix_instance_q15 * pSrcB,
  724. csky_matrix_instance_q15 * pDst);
  725. csky_status csky_mat_add_q31(
  726. const csky_matrix_instance_q31 * pSrcA,
  727. const csky_matrix_instance_q31 * pSrcB,
  728. csky_matrix_instance_q31 * pDst);
  729. csky_status csky_mat_cmplx_mult_f32(
  730. const csky_matrix_instance_f32 * pSrcA,
  731. const csky_matrix_instance_f32 * pSrcB,
  732. csky_matrix_instance_f32 * pDst);
  733. csky_status csky_mat_cmplx_mult_q15(
  734. const csky_matrix_instance_q15 * pSrcA,
  735. const csky_matrix_instance_q15 * pSrcB,
  736. csky_matrix_instance_q15 * pDst,
  737. q15_t * pScratch);
  738. csky_status csky_mat_cmplx_mult_q31(
  739. const csky_matrix_instance_q31 * pSrcA,
  740. const csky_matrix_instance_q31 * pSrcB,
  741. csky_matrix_instance_q31 * pDst);
  742. csky_status csky_mat_trans_f32(
  743. const csky_matrix_instance_f32 * pSrc,
  744. csky_matrix_instance_f32 * pDst);
  745. csky_status csky_mat_trans_q15(
  746. const csky_matrix_instance_q15 * pSrc,
  747. csky_matrix_instance_q15 * pDst);
  748. csky_status csky_mat_trans_q31(
  749. const csky_matrix_instance_q31 * pSrc,
  750. csky_matrix_instance_q31 * pDst);
  751. csky_status csky_mat_mult_f32(
  752. const csky_matrix_instance_f32 * pSrcA,
  753. const csky_matrix_instance_f32 * pSrcB,
  754. csky_matrix_instance_f32 * pDst);
  755. csky_status csky_mat_mult_q15(
  756. const csky_matrix_instance_q15 * pSrcA,
  757. const csky_matrix_instance_q15 * pSrcB,
  758. csky_matrix_instance_q15 * pDst,
  759. q15_t * pState);
  760. csky_status csky_mat_mult_fast_q15(
  761. const csky_matrix_instance_q15 * pSrcA,
  762. const csky_matrix_instance_q15 * pSrcB,
  763. csky_matrix_instance_q15 * pDst,
  764. q15_t * pState);
  765. csky_status csky_mat_mult_q31(
  766. const csky_matrix_instance_q31 * pSrcA,
  767. const csky_matrix_instance_q31 * pSrcB,
  768. csky_matrix_instance_q31 * pDst);
  769. csky_status csky_mat_mult_fast_q31(
  770. const csky_matrix_instance_q31 * pSrcA,
  771. const csky_matrix_instance_q31 * pSrcB,
  772. csky_matrix_instance_q31 * pDst);
  773. csky_status csky_mat_sub_f32(
  774. const csky_matrix_instance_f32 * pSrcA,
  775. const csky_matrix_instance_f32 * pSrcB,
  776. csky_matrix_instance_f32 * pDst);
  777. csky_status csky_mat_sub_q15(
  778. const csky_matrix_instance_q15 * pSrcA,
  779. const csky_matrix_instance_q15 * pSrcB,
  780. csky_matrix_instance_q15 * pDst);
  781. csky_status csky_mat_sub_q31(
  782. const csky_matrix_instance_q31 * pSrcA,
  783. const csky_matrix_instance_q31 * pSrcB,
  784. csky_matrix_instance_q31 * pDst);
  785. csky_status csky_mat_scale_f32(
  786. const csky_matrix_instance_f32 * pSrc,
  787. float32_t scale,
  788. csky_matrix_instance_f32 * pDst);
  789. csky_status csky_mat_scale_q15(
  790. const csky_matrix_instance_q15 * pSrc,
  791. q15_t scaleFract,
  792. int32_t shift,
  793. csky_matrix_instance_q15 * pDst);
  794. csky_status csky_mat_scale_q31(
  795. const csky_matrix_instance_q31 * pSrc,
  796. q31_t scaleFract,
  797. int32_t shift,
  798. csky_matrix_instance_q31 * pDst);
  799. void csky_mat_init_q31(
  800. csky_matrix_instance_q31 * S,
  801. uint16_t nRows,
  802. uint16_t nColumns,
  803. q31_t * pData);
  804. void csky_mat_init_q15(
  805. csky_matrix_instance_q15 * S,
  806. uint16_t nRows,
  807. uint16_t nColumns,
  808. q15_t * pData);
  809. void csky_mat_init_f32(
  810. csky_matrix_instance_f32 * S,
  811. uint16_t nRows,
  812. uint16_t nColumns,
  813. float32_t * pData);
  814. /**
  815. * @brief Instance structure for the Q15 PID Control.
  816. */
  817. typedef struct
  818. {
  819. q15_t A0; /**< The derived gain, A0 = Kp + Ki + Kd . */
  820. q15_t A1;
  821. q15_t A2;
  822. q15_t state[3]; /**< The state array of length 3. */
  823. q15_t Kp; /**< The proportional gain. */
  824. q15_t Ki; /**< The integral gain. */
  825. q15_t Kd; /**< The derivative gain. */
  826. } csky_pid_instance_q15;
  827. /**
  828. * @brief Instance structure for the Q31 PID Control.
  829. */
  830. typedef struct
  831. {
  832. q31_t A0; /**< The derived gain, A0 = Kp + Ki + Kd . */
  833. q31_t A1; /**< The derived gain, A1 = -Kp - 2Kd. */
  834. q31_t A2; /**< The derived gain, A2 = Kd . */
  835. q31_t state[3]; /**< The state array of length 3. */
  836. q31_t Kp; /**< The proportional gain. */
  837. q31_t Ki; /**< The integral gain. */
  838. q31_t Kd; /**< The derivative gain. */
  839. } csky_pid_instance_q31;
  840. /**
  841. * @brief Instance structure for the floating-point PID Control.
  842. */
  843. typedef struct
  844. {
  845. float32_t A0; /**< The derived gain, A0 = Kp + Ki + Kd . */
  846. float32_t A1; /**< The derived gain, A1 = -Kp - 2Kd. */
  847. float32_t A2; /**< The derived gain, A2 = Kd . */
  848. float32_t state[3]; /**< The state array of length 3. */
  849. float32_t Kp; /**< The proportional gain. */
  850. float32_t Ki; /**< The integral gain. */
  851. float32_t Kd; /**< The derivative gain. */
  852. } csky_pid_instance_f32;
  853. void csky_pid_init_f32(
  854. csky_pid_instance_f32 * S,
  855. int32_t resetStateFlag);
  856. void csky_pid_reset_f32(
  857. csky_pid_instance_f32 * S);
  858. void csky_pid_init_q31(
  859. csky_pid_instance_q31 * S,
  860. int32_t resetStateFlag);
  861. void csky_pid_reset_q31(
  862. csky_pid_instance_q31 * S);
  863. void csky_pid_init_q15(
  864. csky_pid_instance_q15 * S,
  865. int32_t resetStateFlag);
  866. void csky_pid_reset_q15(
  867. csky_pid_instance_q15 * S);
  868. /**
  869. * @brief Instance structure for the floating-point Linear Interpolate function.
  870. */
  871. typedef struct
  872. {
  873. uint32_t nValues; /**< nValues */
  874. float32_t x1; /**< x1 */
  875. float32_t xSpacing; /**< xSpacing */
  876. float32_t *pYData; /**< pointer to the table of Y values */
  877. } csky_linear_interp_instance_f32;
  878. /**
  879. * @brief Instance structure for the floating-point bilinear interpolation function.
  880. */
  881. typedef struct
  882. {
  883. uint16_t numRows; /**< number of rows in the data table. */
  884. uint16_t numCols; /**< number of columns in the data table. */
  885. float32_t *pData; /**< points to the data table. */
  886. } csky_bilinear_interp_instance_f32;
  887. /**
  888. * @brief Instance structure for the Q31 bilinear interpolation function.
  889. */
  890. typedef struct
  891. {
  892. uint16_t numRows; /**< number of rows in the data table. */
  893. uint16_t numCols; /**< number of columns in the data table. */
  894. q31_t *pData; /**< points to the data table. */
  895. } csky_bilinear_interp_instance_q31;
  896. /**
  897. * @brief Instance structure for the Q15 bilinear interpolation function.
  898. */
  899. typedef struct
  900. {
  901. uint16_t numRows; /**< number of rows in the data table. */
  902. uint16_t numCols; /**< number of columns in the data table. */
  903. q15_t *pData; /**< points to the data table. */
  904. } csky_bilinear_interp_instance_q15;
  905. /**
  906. * @brief Instance structure for the Q15 bilinear interpolation function.
  907. */
  908. typedef struct
  909. {
  910. uint16_t numRows; /**< number of rows in the data table. */
  911. uint16_t numCols; /**< number of columns in the data table. */
  912. q7_t *pData; /**< points to the data table. */
  913. } csky_bilinear_interp_instance_q7;
  914. void csky_mult_q7(
  915. q7_t * pSrcA,
  916. q7_t * pSrcB,
  917. q7_t * pDst,
  918. uint32_t blockSize);
  919. void csky_mult_q15(
  920. q15_t * pSrcA,
  921. q15_t * pSrcB,
  922. q15_t * pDst,
  923. uint32_t blockSize);
  924. void csky_mult_rnd_q15(
  925. q15_t * pSrcA,
  926. q15_t * pSrcB,
  927. q15_t * pDst,
  928. uint32_t blockSize);
  929. void csky_mult_q31(
  930. q31_t * pSrcA,
  931. q31_t * pSrcB,
  932. q31_t * pDst,
  933. uint32_t blockSize);
  934. void csky_mult_f32(
  935. float32_t * pSrcA,
  936. float32_t * pSrcB,
  937. float32_t * pDst,
  938. uint32_t blockSize);
  939. /**
  940. * @brief Instance structure for the Q15 CFFT/CIFFT function.
  941. */
  942. typedef struct
  943. {
  944. uint16_t fftLen; /**< length of the FFT. */
  945. uint8_t ifftFlag; /**< flag that selects forward (ifftFlag=0) or inverse (ifftFlag=1) transform. */
  946. uint8_t bitReverseFlag; /**< flag that enables (bitReverseFlag=1) or disables (bitReverseFlag=0) bit reversal of output. */
  947. q15_t *pTwiddle; /**< points to the Sin twiddle factor table. */
  948. uint16_t *pBitRevTable; /**< points to the bit reversal table. */
  949. uint16_t twidCoefModifier; /**< twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table. */
  950. uint16_t bitRevFactor; /**< bit reversal modifier that supports different size FFTs with the same bit reversal table. */
  951. } csky_cfft_radix2_instance_q15;
  952. /**
  953. * @brief Instance structure for the Q15 CFFT/CIFFT function.
  954. */
  955. typedef struct
  956. {
  957. uint16_t fftLen; /**< length of the FFT. */
  958. uint8_t ifftFlag; /**< flag that selects forward (ifftFlag=0) or inverse (ifftFlag=1) transform. */
  959. uint8_t bitReverseFlag; /**< flag that enables (bitReverseFlag=1) or disables (bitReverseFlag=0) bit reversal of output. */
  960. q15_t *pTwiddle; /**< points to the twiddle factor table. */
  961. uint16_t *pBitRevTable; /**< points to the bit reversal table. */
  962. uint16_t twidCoefModifier; /**< twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table. */
  963. uint16_t bitRevFactor; /**< bit reversal modifier that supports different size FFTs with the same bit reversal table. */
  964. } csky_cfft_radix4_instance_q15;
  965. /**
  966. * @brief Instance structure for the Radix-2 Q31 CFFT/CIFFT function.
  967. */
  968. typedef struct
  969. {
  970. uint16_t fftLen; /**< length of the FFT. */
  971. uint8_t ifftFlag; /**< flag that selects forward (ifftFlag=0) or inverse (ifftFlag=1) transform. */
  972. uint8_t bitReverseFlag; /**< flag that enables (bitReverseFlag=1) or disables (bitReverseFlag=0) bit reversal of output. */
  973. q31_t *pTwiddle; /**< points to the Twiddle factor table. */
  974. uint16_t *pBitRevTable; /**< points to the bit reversal table. */
  975. uint16_t twidCoefModifier; /**< twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table. */
  976. uint16_t bitRevFactor; /**< bit reversal modifier that supports different size FFTs with the same bit reversal table. */
  977. } csky_cfft_radix2_instance_q31;
  978. /**
  979. * @brief Instance structure for the Q31 CFFT/CIFFT function.
  980. */
  981. typedef struct
  982. {
  983. uint16_t fftLen; /**< length of the FFT. */
  984. uint8_t ifftFlag; /**< flag that selects forward (ifftFlag=0) or inverse (ifftFlag=1) transform. */
  985. uint8_t bitReverseFlag; /**< flag that enables (bitReverseFlag=1) or disables (bitReverseFlag=0) bit reversal of output. */
  986. q31_t *pTwiddle; /**< points to the twiddle factor table. */
  987. uint16_t *pBitRevTable; /**< points to the bit reversal table. */
  988. uint16_t twidCoefModifier; /**< twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table. */
  989. uint16_t bitRevFactor; /**< bit reversal modifier that supports different size FFTs with the same bit reversal table. */
  990. } csky_cfft_radix4_instance_q31;
  991. /**
  992. * @brief Instance structure for the floating-point CFFT/CIFFT function.
  993. */
  994. typedef struct
  995. {
  996. uint16_t fftLen; /**< length of the FFT. */
  997. uint8_t ifftFlag; /**< flag that selects forward (ifftFlag=0) or inverse (ifftFlag=1) transform. */
  998. uint8_t bitReverseFlag; /**< flag that enables (bitReverseFlag=1) or disables (bitReverseFlag=0) bit reversal of output. */
  999. float32_t *pTwiddle; /**< points to the Twiddle factor table. */
  1000. uint16_t *pBitRevTable; /**< points to the bit reversal table. */
  1001. uint16_t twidCoefModifier; /**< twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table. */
  1002. uint16_t bitRevFactor; /**< bit reversal modifier that supports different size FFTs with the same bit reversal table. */
  1003. float32_t onebyfftLen; /**< value of 1/fftLen. */
  1004. } csky_cfft_radix2_instance_f32;
  1005. /**
  1006. * @brief Instance structure for the floating-point CFFT/CIFFT function.
  1007. */
  1008. typedef struct
  1009. {
  1010. uint16_t fftLen; /**< length of the FFT. */
  1011. uint8_t ifftFlag; /**< flag that selects forward (ifftFlag=0) or inverse (ifftFlag=1) transform. */
  1012. uint8_t bitReverseFlag; /**< flag that enables (bitReverseFlag=1) or disables (bitReverseFlag=0) bit reversal of output. */
  1013. float32_t *pTwiddle; /**< points to the Twiddle factor table. */
  1014. uint16_t *pBitRevTable; /**< points to the bit reversal table. */
  1015. uint16_t twidCoefModifier; /**< twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table. */
  1016. uint16_t bitRevFactor; /**< bit reversal modifier that supports different size FFTs with the same bit reversal table. */
  1017. float32_t onebyfftLen; /**< value of 1/fftLen. */
  1018. } csky_cfft_radix4_instance_f32;
  1019. /**
  1020. * @brief Instance structure for the fixed-point CFFT/CIFFT function.
  1021. */
  1022. typedef struct
  1023. {
  1024. uint16_t fftLen; /**< length of the FFT. */
  1025. const q15_t *pTwiddle; /**< points to the Twiddle factor table. */
  1026. const uint16_t *pBitRevTable; /**< points to the bit reversal table. */
  1027. uint16_t bitRevLength; /**< bit reversal table length. */
  1028. } csky_cfft_instance_q15;
  1029. void csky_cfft_q15(
  1030. const csky_cfft_instance_q15 * S,
  1031. q15_t * p1,
  1032. uint8_t ifftFlag,
  1033. uint8_t bitReverseFlag);
  1034. /**
  1035. * @brief Instance structure for the fixed-point CFFT/CIFFT function.
  1036. */
  1037. typedef struct
  1038. {
  1039. uint16_t fftLen; /**< length of the FFT. */
  1040. const q31_t *pTwiddle; /**< points to the Twiddle factor table. */
  1041. const uint16_t *pBitRevTable; /**< points to the bit reversal table. */
  1042. uint16_t bitRevLength; /**< bit reversal table length. */
  1043. } csky_cfft_instance_q31;
  1044. void csky_cfft_q31(
  1045. const csky_cfft_instance_q31 * S,
  1046. q31_t * p1,
  1047. uint8_t ifftFlag,
  1048. uint8_t bitReverseFlag);
  1049. /**
  1050. * @brief Instance structure for the floating-point CFFT/CIFFT function.
  1051. */
  1052. typedef struct
  1053. {
  1054. uint16_t fftLen; /**< length of the FFT. */
  1055. const float32_t *pTwiddle; /**< points to the Twiddle factor table. */
  1056. const uint16_t *pBitRevTable; /**< points to the bit reversal table. */
  1057. uint16_t bitRevLength; /**< bit reversal table length. */
  1058. } csky_cfft_instance_f32;
  1059. void csky_cfft_f32(
  1060. const csky_cfft_instance_f32 * S,
  1061. float32_t * p1,
  1062. uint8_t ifftFlag,
  1063. uint8_t bitReverseFlag);
  1064. /**
  1065. * @brief Instance structure for the Q15 RFFT/RIFFT function.
  1066. */
  1067. typedef struct
  1068. {
  1069. uint32_t fftLenReal; /**< length of the real FFT. */
  1070. uint8_t ifftFlagR; /**< flag that selects forward (ifftFlagR=0) or inverse (ifftFlagR=1) transform. */
  1071. uint8_t bitReverseFlagR; /**< flag that enables (bitReverseFlagR=1) or disables (bitReverseFlagR=0) bit reversal of output. */
  1072. uint32_t twidCoefRModifier; /**< twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table. */
  1073. q15_t *pTwiddleAReal; /**< points to the real twiddle factor table. */
  1074. const csky_cfft_instance_q15 *pCfft; /**< points to the complex FFT instance. */
  1075. } csky_rfft_instance_q15;
  1076. csky_status csky_rfft_init_q15(
  1077. csky_rfft_instance_q15 * S,
  1078. uint32_t fftLenReal,
  1079. uint32_t ifftFlagR,
  1080. uint32_t bitReverseFlag);
  1081. void csky_rfft_q15(
  1082. const csky_rfft_instance_q15 * S,
  1083. q15_t * pSrc,
  1084. q15_t * pDst);
  1085. /**
  1086. * @brief Instance structure for the Q31 RFFT/RIFFT function.
  1087. */
  1088. typedef struct
  1089. {
  1090. uint32_t fftLenReal; /**< length of the real FFT. */
  1091. uint8_t ifftFlagR; /**< flag that selects forward (ifftFlagR=0) or inverse (ifftFlagR=1) transform. */
  1092. uint8_t bitReverseFlagR; /**< flag that enables (bitReverseFlagR=1) or disables (bitReverseFlagR=0) bit reversal of output. */
  1093. uint32_t twidCoefRModifier; /**< twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table. */
  1094. q31_t *pTwiddleAReal; /**< points to the real twiddle factor table. */
  1095. const csky_cfft_instance_q31 *pCfft; /**< points to the complex FFT instance. */
  1096. } csky_rfft_instance_q31;
  1097. csky_status csky_rfft_init_q31(
  1098. csky_rfft_instance_q31 * S,
  1099. uint32_t fftLenReal,
  1100. uint32_t ifftFlagR,
  1101. uint32_t bitReverseFlag);
  1102. void csky_rfft_q31(
  1103. const csky_rfft_instance_q31 * S,
  1104. q31_t * pSrc,
  1105. q31_t * pDst);
  1106. /**
  1107. * @brief Instance structure for the floating-point RFFT/RIFFT function.
  1108. */
  1109. typedef struct
  1110. {
  1111. uint32_t fftLenReal; /**< length of the real FFT. */
  1112. uint16_t fftLenBy2; /**< length of the complex FFT. */
  1113. uint8_t ifftFlagR; /**< flag that selects forward (ifftFlagR=0) or inverse (ifftFlagR=1) transform. */
  1114. uint8_t bitReverseFlagR; /**< flag that enables (bitReverseFlagR=1) or disables (bitReverseFlagR=0) bit reversal of output. */
  1115. uint32_t twidCoefRModifier; /**< twiddle coefficient modifier that supports different size FFTs with the same twiddle factor table. */
  1116. float32_t *pTwiddleAReal; /**< points to the real twiddle factor table. */
  1117. float32_t *pTwiddleBReal; /**< points to the imag twiddle factor table. */
  1118. csky_cfft_radix4_instance_f32 *pCfft; /**< points to the complex FFT instance. */
  1119. } csky_rfft_instance_f32;
  1120. csky_status csky_rfft_init_f32(
  1121. csky_rfft_instance_f32 * S,
  1122. csky_cfft_radix4_instance_f32 * S_CFFT,
  1123. uint32_t fftLenReal,
  1124. uint32_t ifftFlagR,
  1125. uint32_t bitReverseFlag);
  1126. void csky_rfft_f32(
  1127. const csky_rfft_instance_f32 * S,
  1128. float32_t * pSrc,
  1129. float32_t * pDst);
  1130. /**
  1131. * @brief Instance structure for the floating-point RFFT/RIFFT function.
  1132. */
  1133. typedef struct
  1134. {
  1135. csky_cfft_instance_f32 Sint; /**< Internal CFFT structure. */
  1136. uint16_t fftLenRFFT; /**< length of the real sequence */
  1137. float32_t * pTwiddleRFFT; /**< Twiddle factors real stage */
  1138. } csky_rfft_fast_instance_f32 ;
  1139. csky_status csky_rfft_fast_init_f32 (
  1140. csky_rfft_fast_instance_f32 * S,
  1141. uint16_t fftLen);
  1142. void csky_rfft_fast_f32(
  1143. csky_rfft_fast_instance_f32 * S,
  1144. float32_t * p, float32_t * pOut,
  1145. uint8_t ifftFlag);
  1146. /**
  1147. * @brief Instance structure for the floating-point DCT4/IDCT4 function.
  1148. */
  1149. typedef struct
  1150. {
  1151. uint16_t N; /**< length of the DCT4. */
  1152. uint16_t Nby2; /**< half of the length of the DCT4. */
  1153. float32_t normalize; /**< normalizing factor. */
  1154. float32_t *pTwiddle; /**< points to the twiddle factor table. */
  1155. float32_t *pCosFactor; /**< points to the cosFactor table. */
  1156. csky_rfft_fast_instance_f32 *pRfft; /**< points to the real FFT fast instance. */
  1157. csky_cfft_radix4_instance_f32 *pCfft; /**< points to the complex FFT instance. */
  1158. } csky_dct4_instance_f32;
  1159. csky_status csky_dct4_init_f32(
  1160. csky_dct4_instance_f32 * S,
  1161. csky_rfft_fast_instance_f32 * S_RFFT,
  1162. csky_cfft_radix4_instance_f32 * S_CFFT,
  1163. uint16_t N,
  1164. uint16_t Nby2,
  1165. float32_t normalize);
  1166. void csky_dct4_f32(
  1167. const csky_dct4_instance_f32 * S,
  1168. float32_t * pState,
  1169. float32_t * pInlineBuffer);
  1170. /**
  1171. * @brief Instance structure for the Q31 DCT4/IDCT4 function.
  1172. */
  1173. typedef struct
  1174. {
  1175. uint16_t N; /**< length of the DCT4. */
  1176. uint16_t Nby2; /**< half of the length of the DCT4. */
  1177. q31_t normalize; /**< normalizing factor. */
  1178. q31_t *pTwiddle; /**< points to the twiddle factor table. */
  1179. q31_t *pCosFactor; /**< points to the cosFactor table. */
  1180. csky_rfft_instance_q31 *pRfft; /**< points to the real FFT instance. */
  1181. csky_cfft_radix4_instance_q31 *pCfft; /**< points to the complex FFT instance. */
  1182. } csky_dct4_instance_q31;
  1183. csky_status csky_dct4_init_q31(
  1184. csky_dct4_instance_q31 * S,
  1185. csky_rfft_instance_q31 * S_RFFT,
  1186. csky_cfft_radix4_instance_q31 * S_CFFT,
  1187. uint16_t N,
  1188. uint16_t Nby2,
  1189. q31_t normalize);
  1190. void csky_dct4_q31(
  1191. const csky_dct4_instance_q31 * S,
  1192. q31_t * pState,
  1193. q31_t * pInlineBuffer);
  1194. /**
  1195. * @brief Instance structure for the Q15 DCT4/IDCT4 function.
  1196. */
  1197. typedef struct
  1198. {
  1199. uint16_t N; /**< length of the DCT4. */
  1200. uint16_t Nby2; /**< half of the length of the DCT4. */
  1201. q15_t normalize; /**< normalizing factor. */
  1202. q15_t *pTwiddle; /**< points to the twiddle factor table. */
  1203. q15_t *pCosFactor; /**< points to the cosFactor table. */
  1204. csky_rfft_instance_q15 *pRfft; /**< points to the real FFT instance. */
  1205. csky_cfft_radix4_instance_q15 *pCfft; /**< points to the complex FFT instance. */
  1206. } csky_dct4_instance_q15;
  1207. csky_status csky_dct4_init_q15(
  1208. csky_dct4_instance_q15 * S,
  1209. csky_rfft_instance_q15 * S_RFFT,
  1210. csky_cfft_radix4_instance_q15 * S_CFFT,
  1211. uint16_t N,
  1212. uint16_t Nby2,
  1213. q15_t normalize);
  1214. void csky_dct4_q15(
  1215. const csky_dct4_instance_q15 * S,
  1216. q15_t * pState,
  1217. q15_t * pInlineBuffer);
  1218. void csky_add_f32(
  1219. float32_t * pSrcA,
  1220. float32_t * pSrcB,
  1221. float32_t * pDst,
  1222. uint32_t blockSize);
  1223. void csky_add_q7(
  1224. q7_t * pSrcA,
  1225. q7_t * pSrcB,
  1226. q7_t * pDst,
  1227. uint32_t blockSize);
  1228. void csky_add_q15(
  1229. q15_t * pSrcA,
  1230. q15_t * pSrcB,
  1231. q15_t * pDst,
  1232. uint32_t blockSize);
  1233. void csky_add_q31(
  1234. q31_t * pSrcA,
  1235. q31_t * pSrcB,
  1236. q31_t * pDst,
  1237. uint32_t blockSize);
  1238. void csky_sub_f32(
  1239. float32_t * pSrcA,
  1240. float32_t * pSrcB,
  1241. float32_t * pDst,
  1242. uint32_t blockSize);
  1243. void csky_sub_q7(
  1244. q7_t * pSrcA,
  1245. q7_t * pSrcB,
  1246. q7_t * pDst,
  1247. uint32_t blockSize);
  1248. void csky_sub_q15(
  1249. q15_t * pSrcA,
  1250. q15_t * pSrcB,
  1251. q15_t * pDst,
  1252. uint32_t blockSize);
  1253. void csky_sub_q31(
  1254. q31_t * pSrcA,
  1255. q31_t * pSrcB,
  1256. q31_t * pDst,
  1257. uint32_t blockSize);
  1258. void csky_scale_f32(
  1259. float32_t * pSrc,
  1260. float32_t scale,
  1261. float32_t * pDst,
  1262. uint32_t blockSize);
  1263. void csky_scale_q7(
  1264. q7_t * pSrc,
  1265. q7_t scaleFract,
  1266. int8_t shift,
  1267. q7_t * pDst,
  1268. uint32_t blockSize);
  1269. void csky_scale_q15(
  1270. q15_t * pSrc,
  1271. q15_t scaleFract,
  1272. int8_t shift,
  1273. q15_t * pDst,
  1274. uint32_t blockSize);
  1275. void csky_scale_q31(
  1276. q31_t * pSrc,
  1277. q31_t scaleFract,
  1278. int8_t shift,
  1279. q31_t * pDst,
  1280. uint32_t blockSize);
  1281. void csky_abs_q7(
  1282. q7_t * pSrc,
  1283. q7_t * pDst,
  1284. uint32_t blockSize);
  1285. void csky_abs_f32(
  1286. float32_t * pSrc,
  1287. float32_t * pDst,
  1288. uint32_t blockSize);
  1289. void csky_abs_q15(
  1290. q15_t * pSrc,
  1291. q15_t * pDst,
  1292. uint32_t blockSize);
  1293. void csky_abs_q31(
  1294. q31_t * pSrc,
  1295. q31_t * pDst,
  1296. uint32_t blockSize);
  1297. void csky_abs_max_q15(
  1298. q15_t * pSrc,
  1299. q15_t * pDst,
  1300. uint32_t blockSize);
  1301. void csky_abs_max_q31(
  1302. q31_t * pSrc,
  1303. q31_t * pDst,
  1304. uint32_t blockSize);
  1305. void csky_dot_prod_f32(
  1306. float32_t * pSrcA,
  1307. float32_t * pSrcB,
  1308. uint32_t blockSize,
  1309. float32_t * result);
  1310. void csky_dot_prod_q7(
  1311. q7_t * pSrcA,
  1312. q7_t * pSrcB,
  1313. uint32_t blockSize,
  1314. q31_t * result);
  1315. void csky_dot_prod_q15(
  1316. q15_t * pSrcA,
  1317. q15_t * pSrcB,
  1318. uint32_t blockSize,
  1319. q63_t * result);
  1320. void csky_dot_prod_q31(
  1321. q31_t * pSrcA,
  1322. q31_t * pSrcB,
  1323. uint32_t blockSize,
  1324. q63_t * result);
  1325. void csky_shift_q7(
  1326. q7_t * pSrc,
  1327. int8_t shiftBits,
  1328. q7_t * pDst,
  1329. uint32_t blockSize);
  1330. void csky_shift_q15(
  1331. q15_t * pSrc,
  1332. int8_t shiftBits,
  1333. q15_t * pDst,
  1334. uint32_t blockSize);
  1335. void csky_shift_q31(
  1336. q31_t * pSrc,
  1337. int8_t shiftBits,
  1338. q31_t * pDst,
  1339. uint32_t blockSize);
  1340. void csky_offset_f32(
  1341. float32_t * pSrc,
  1342. float32_t offset,
  1343. float32_t * pDst,
  1344. uint32_t blockSize);
  1345. void csky_offset_q7(
  1346. q7_t * pSrc,
  1347. q7_t offset,
  1348. q7_t * pDst,
  1349. uint32_t blockSize);
  1350. void csky_offset_q15(
  1351. q15_t * pSrc,
  1352. q15_t offset,
  1353. q15_t * pDst,
  1354. uint32_t blockSize);
  1355. void csky_offset_q31(
  1356. q31_t * pSrc,
  1357. q31_t offset,
  1358. q31_t * pDst,
  1359. uint32_t blockSize);
  1360. void csky_negate_f32(
  1361. float32_t * pSrc,
  1362. float32_t * pDst,
  1363. uint32_t blockSize);
  1364. void csky_negate_q7(
  1365. q7_t * pSrc,
  1366. q7_t * pDst,
  1367. uint32_t blockSize);
  1368. void csky_negate_q15(
  1369. q15_t * pSrc,
  1370. q15_t * pDst,
  1371. uint32_t blockSize);
  1372. void csky_negate_q31(
  1373. q31_t * pSrc,
  1374. q31_t * pDst,
  1375. uint32_t blockSize);
  1376. void csky_copy_f32(
  1377. float32_t * pSrc,
  1378. float32_t * pDst,
  1379. uint32_t blockSize);
  1380. void csky_copy_q7(
  1381. q7_t * pSrc,
  1382. q7_t * pDst,
  1383. uint32_t blockSize);
  1384. void csky_copy_q15(
  1385. q15_t * pSrc,
  1386. q15_t * pDst,
  1387. uint32_t blockSize);
  1388. void csky_copy_q31(
  1389. q31_t * pSrc,
  1390. q31_t * pDst,
  1391. uint32_t blockSize);
  1392. void csky_fill_f32(
  1393. float32_t value,
  1394. float32_t * pDst,
  1395. uint32_t blockSize);
  1396. void csky_fill_q7(
  1397. q7_t value,
  1398. q7_t * pDst,
  1399. uint32_t blockSize);
  1400. void csky_fill_q15(
  1401. q15_t value,
  1402. q15_t * pDst,
  1403. uint32_t blockSize);
  1404. void csky_fill_q31(
  1405. q31_t value,
  1406. q31_t * pDst,
  1407. uint32_t blockSize);
  1408. void csky_conv_f32(
  1409. float32_t * pSrcA,
  1410. uint32_t srcALen,
  1411. float32_t * pSrcB,
  1412. uint32_t srcBLen,
  1413. float32_t * pDst);
  1414. void csky_conv_opt_q15(
  1415. q15_t * pSrcA,
  1416. uint32_t srcALen,
  1417. q15_t * pSrcB,
  1418. uint32_t srcBLen,
  1419. q15_t * pDst,
  1420. q15_t * pScratch1,
  1421. q15_t * pScratch2);
  1422. void csky_conv_q15(
  1423. q15_t * pSrcA,
  1424. uint32_t srcALen,
  1425. q15_t * pSrcB,
  1426. uint32_t srcBLen,
  1427. q15_t * pDst);
  1428. void csky_conv_fast_q15(
  1429. q15_t * pSrcA,
  1430. uint32_t srcALen,
  1431. q15_t * pSrcB,
  1432. uint32_t srcBLen,
  1433. q15_t * pDst);
  1434. void csky_conv_fast_opt_q15(
  1435. q15_t * pSrcA,
  1436. uint32_t srcALen,
  1437. q15_t * pSrcB,
  1438. uint32_t srcBLen,
  1439. q15_t * pDst,
  1440. q15_t * pScratch1,
  1441. q15_t * pScratch2);
  1442. void csky_conv_q31(
  1443. q31_t * pSrcA,
  1444. uint32_t srcALen,
  1445. q31_t * pSrcB,
  1446. uint32_t srcBLen,
  1447. q31_t * pDst);
  1448. void csky_conv_fast_q31(
  1449. q31_t * pSrcA,
  1450. uint32_t srcALen,
  1451. q31_t * pSrcB,
  1452. uint32_t srcBLen,
  1453. q31_t * pDst);
  1454. void csky_conv_opt_q7(
  1455. q7_t * pSrcA,
  1456. uint32_t srcALen,
  1457. q7_t * pSrcB,
  1458. uint32_t srcBLen,
  1459. q7_t * pDst,
  1460. q15_t * pScratch1,
  1461. q15_t * pScratch2);
  1462. void csky_conv_q7(
  1463. q7_t * pSrcA,
  1464. uint32_t srcALen,
  1465. q7_t * pSrcB,
  1466. uint32_t srcBLen,
  1467. q7_t * pDst);
  1468. csky_status csky_conv_partial_f32(
  1469. float32_t * pSrcA,
  1470. uint32_t srcALen,
  1471. float32_t * pSrcB,
  1472. uint32_t srcBLen,
  1473. float32_t * pDst,
  1474. uint32_t firstIndex,
  1475. uint32_t numPoints);
  1476. csky_status csky_conv_partial_opt_q15(
  1477. q15_t * pSrcA,
  1478. uint32_t srcALen,
  1479. q15_t * pSrcB,
  1480. uint32_t srcBLen,
  1481. q15_t * pDst,
  1482. uint32_t firstIndex,
  1483. uint32_t numPoints,
  1484. q15_t * pScratch1,
  1485. q15_t * pScratch2);
  1486. csky_status csky_conv_partial_q15(
  1487. q15_t * pSrcA,
  1488. uint32_t srcALen,
  1489. q15_t * pSrcB,
  1490. uint32_t srcBLen,
  1491. q15_t * pDst,
  1492. uint32_t firstIndex,
  1493. uint32_t numPoints);
  1494. csky_status csky_conv_partial_fast_q15(
  1495. q15_t * pSrcA,
  1496. uint32_t srcALen,
  1497. q15_t * pSrcB,
  1498. uint32_t srcBLen,
  1499. q15_t * pDst,
  1500. uint32_t firstIndex,
  1501. uint32_t numPoints);
  1502. csky_status csky_conv_partial_fast_opt_q15(
  1503. q15_t * pSrcA,
  1504. uint32_t srcALen,
  1505. q15_t * pSrcB,
  1506. uint32_t srcBLen,
  1507. q15_t * pDst,
  1508. uint32_t firstIndex,
  1509. uint32_t numPoints,
  1510. q15_t * pScratch1,
  1511. q15_t * pScratch2);
  1512. csky_status csky_conv_partial_q31(
  1513. q31_t * pSrcA,
  1514. uint32_t srcALen,
  1515. q31_t * pSrcB,
  1516. uint32_t srcBLen,
  1517. q31_t * pDst,
  1518. uint32_t firstIndex,
  1519. uint32_t numPoints);
  1520. csky_status csky_conv_partial_fast_q31(
  1521. q31_t * pSrcA,
  1522. uint32_t srcALen,
  1523. q31_t * pSrcB,
  1524. uint32_t srcBLen,
  1525. q31_t * pDst,
  1526. uint32_t firstIndex,
  1527. uint32_t numPoints);
  1528. csky_status csky_conv_partial_opt_q7(
  1529. q7_t * pSrcA,
  1530. uint32_t srcALen,
  1531. q7_t * pSrcB,
  1532. uint32_t srcBLen,
  1533. q7_t * pDst,
  1534. uint32_t firstIndex,
  1535. uint32_t numPoints,
  1536. q15_t * pScratch1,
  1537. q15_t * pScratch2);
  1538. csky_status csky_conv_partial_q7(
  1539. q7_t * pSrcA,
  1540. uint32_t srcALen,
  1541. q7_t * pSrcB,
  1542. uint32_t srcBLen,
  1543. q7_t * pDst,
  1544. uint32_t firstIndex,
  1545. uint32_t numPoints);
  1546. /**
  1547. * functions for the yunVoice functions.
  1548. */
  1549. q15_t csky_dsp_lib_vec_max_abs16(
  1550. q15_t * A,
  1551. uint32_t N);
  1552. q31_t csky_dsp_lib_vec_max_abs32(
  1553. q31_t * A,
  1554. uint32_t N);
  1555. void csky_dsp_lib_vec_abs16(
  1556. q15_t * A,
  1557. uint32_t N,
  1558. q15_t * C);
  1559. void csky_dsp_lib_vec_abs32(
  1560. q31_t * A,
  1561. uint32_t N,
  1562. q31_t * C);
  1563. void csky_dsp_lib_vec_add16(
  1564. q15_t * A,
  1565. q15_t * B,
  1566. uint32_t N,
  1567. q15_t * C);
  1568. void csky_dsp_lib_vec_add32(
  1569. q31_t * A,
  1570. q31_t * B,
  1571. uint32_t N,
  1572. q31_t * C);
  1573. void csky_dsp_lib_vec_cx_conj_q15(
  1574. q15_t * A,
  1575. uint32_t N,
  1576. q15_t * B);
  1577. void csky_dsp_lib_vec_cx_conj_q31(
  1578. q31_t * A,
  1579. uint32_t N,
  1580. q31_t * C);
  1581. q31_t csky_dsp_lib_vec_dot_q15(
  1582. q15_t * A,
  1583. q15_t * B,
  1584. uint32_t N);
  1585. q31_t csky_dsp_lib_vec_dot_q31(
  1586. q31_t * A,
  1587. q31_t * B,
  1588. uint32_t N);
  1589. void csky_dsp_lib_mat_cx_add16(
  1590. cq15_t * A,
  1591. cq15_t * B,
  1592. uint32_t N,
  1593. uint32_t M,
  1594. cq15_t * C);
  1595. void csky_dsp_lib_mat_cx_add32(
  1596. cq31_t * A,
  1597. cq31_t * B,
  1598. uint32_t N,
  1599. uint32_t M,
  1600. cq31_t * C);
  1601. void csky_dsp_lib_mat_cx_mul_q15(
  1602. cq15_t * A,
  1603. cq15_t * B,
  1604. uint32_t N,
  1605. uint32_t M,
  1606. uint32_t L,
  1607. cq15_t * C);
  1608. void csky_dsp_lib_mat_cx_mul_q31(
  1609. cq31_t * A,
  1610. cq31_t * B,
  1611. uint32_t N,
  1612. uint32_t M,
  1613. uint32_t L,
  1614. cq31_t * C);
  1615. void csky_dsp_lib_mat_cx_sub16(
  1616. cq15_t * A,
  1617. cq15_t * B,
  1618. uint32_t N,
  1619. uint32_t M,
  1620. cq15_t * C);
  1621. void csky_dsp_lib_mat_cx_sub32(
  1622. cq31_t * A,
  1623. cq31_t * B,
  1624. uint32_t N,
  1625. uint32_t M,
  1626. cq31_t * C);
  1627. void csky_dsp_lib_vec_mul_q15(
  1628. q15_t * A,
  1629. q15_t * B,
  1630. uint32_t N,
  1631. q15_t * C);
  1632. void csky_dsp_lib_vec_mul_q31(
  1633. q31_t * A,
  1634. q31_t * B,
  1635. uint32_t N,
  1636. q31_t * C);
  1637. q31_t csky_dsp_lib_pow_int32(
  1638. q31_t arg_in_x,
  1639. q15_t arg_exp_in_x,
  1640. q31_t arg_in_y,
  1641. q15_t arg_exp_in_y,
  1642. q31_t *arg_exp_out);
  1643. void csky_dsp_lib_vec_scale_q15(
  1644. q15_t * A,
  1645. q15_t scaleFract,
  1646. int8_t shift,
  1647. q15_t * B,
  1648. uint32_t N);
  1649. void csky_dsp_lib_vec_scale_q31(
  1650. q31_t * A,
  1651. q31_t scaleFract,
  1652. int8_t shift,
  1653. q31_t * B,
  1654. uint32_t N);
  1655. void csky_dsp_lib_vec_shf16(
  1656. q15_t * A,
  1657. int8_t shift_val,
  1658. uint32_t N,
  1659. q15_t * C);
  1660. void csky_dsp_lib_vec_shf32(
  1661. q31_t * A,
  1662. q31_t shift_val,
  1663. uint32_t N,
  1664. q31_t * C);
  1665. q15_t csky_dsp_lib_sqrt_int32(
  1666. q31_t x,
  1667. uint32_t rnd_flag);
  1668. void csky_dsp_lib_vec_sub16(
  1669. q15_t * A,
  1670. q15_t * B,
  1671. uint32_t N,
  1672. q15_t * C);
  1673. void csky_dsp_lib_vec_sub32(
  1674. q31_t * A,
  1675. q31_t * B,
  1676. uint32_t N,
  1677. q31_t * C);
  1678. q63_t csky_dsp_lib_vec_sum16(
  1679. q15_t * A,
  1680. uint32_t N);
  1681. q63_t csky_dsp_lib_vec_sum32(
  1682. q31_t * A,
  1683. uint32_t N);
  1684. void csky_fft_lib_cx16_fft(
  1685. q31_t log2_buf_len,
  1686. q15_t * in_buf,
  1687. q15_t * out_buf,
  1688. const q15_t * twi_table,
  1689. const uint16_t * bitrev_tbl,
  1690. q15_t * temp_buf,
  1691. q7_t * ScaleShift,
  1692. q31_t br);
  1693. void csky_fft_lib_cx32_fft(
  1694. q31_t log2_buf_len,
  1695. q31_t * in_buf,
  1696. q31_t * out_buf,
  1697. const q31_t * twi_table,
  1698. const uint16_t * bitrev_tbl,
  1699. q31_t * temp_buf,
  1700. q31_t br);
  1701. void csky_fft_lib_cx16_ifft(
  1702. q31_t log2_buf_len,
  1703. q15_t * in_buf,
  1704. q15_t * out_buf,
  1705. const q15_t * twi_table,
  1706. const uint16_t * bitrev_tbl,
  1707. q15_t * temp_buf,
  1708. q7_t * ScaleShift,
  1709. q31_t br);
  1710. void csky_fft_lib_cx32_ifft(
  1711. q31_t log2_buf_len,
  1712. q31_t * in_buf,
  1713. q31_t * out_buf,
  1714. const q31_t * twi_table,
  1715. const uint16_t * bitrev_tbl,
  1716. q31_t * temp_buf,
  1717. q31_t br);
  1718. void csky_fft_lib_int16_fft(
  1719. q31_t log2_buf_len,
  1720. q15_t * in_buf,
  1721. q15_t * out_buf,
  1722. const q15_t * twi_table,
  1723. const q15_t * last_stage_twi_table,
  1724. const uint16_t * bitrev_tbl,
  1725. q15_t * temp_buf,
  1726. q7_t * ScaleShift,
  1727. q31_t br);
  1728. void csky_fft_lib_int32_fft(
  1729. q31_t log2_buf_len,
  1730. q31_t * in_buf,
  1731. q31_t * out_buf,
  1732. const q31_t * twi_table,
  1733. const q31_t * last_stage_twi_table,
  1734. const uint16_t * bitrev_tbl,
  1735. q31_t * temp_buf,
  1736. q31_t br);
  1737. void csky_fft_lib_int16_ifft(
  1738. q31_t log2_buf_len,
  1739. q15_t * in_buf,
  1740. q15_t * out_buf,
  1741. const q15_t * twi_table,
  1742. const q15_t * last_stage_twi_table,
  1743. const uint16_t * bitrev_tbl,
  1744. q15_t * temp_buf,
  1745. q7_t * ScaleShift,
  1746. q31_t br);
  1747. void csky_fft_lib_int32_ifft(
  1748. q31_t log2_buf_len,
  1749. q31_t * in_buf,
  1750. q31_t * out_buf,
  1751. const q31_t * twi_table,
  1752. const q31_t * last_stage_twi_table,
  1753. const uint16_t * bitrev_tbl,
  1754. q31_t * temp_buf,
  1755. q31_t br);
  1756. /**
  1757. * @brief Instance structure for the Q15 FIR decimator.
  1758. */
  1759. typedef struct
  1760. {
  1761. uint8_t M; /**< decimation factor. */
  1762. uint16_t numTaps; /**< number of coefficients in the filter. */
  1763. q15_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps.*/
  1764. q15_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  1765. } csky_fir_decimate_instance_q15;
  1766. /**
  1767. * @brief Instance structure for the Q31 FIR decimator.
  1768. */
  1769. typedef struct
  1770. {
  1771. uint8_t M; /**< decimation factor. */
  1772. uint16_t numTaps; /**< number of coefficients in the filter. */
  1773. q31_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps.*/
  1774. q31_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  1775. } csky_fir_decimate_instance_q31;
  1776. /**
  1777. * @brief Instance structure for the floating-point FIR decimator.
  1778. */
  1779. typedef struct
  1780. {
  1781. uint8_t M; /**< decimation factor. */
  1782. uint16_t numTaps; /**< number of coefficients in the filter. */
  1783. float32_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps.*/
  1784. float32_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  1785. } csky_fir_decimate_instance_f32;
  1786. void csky_fir_decimate_f32(
  1787. const csky_fir_decimate_instance_f32 * S,
  1788. float32_t * pSrc,
  1789. float32_t * pDst,
  1790. uint32_t blockSize);
  1791. csky_status csky_fir_decimate_init_f32(
  1792. csky_fir_decimate_instance_f32 * S,
  1793. uint16_t numTaps,
  1794. uint8_t M,
  1795. float32_t * pCoeffs,
  1796. float32_t * pState,
  1797. uint32_t blockSize);
  1798. void csky_fir_decimate_q15(
  1799. const csky_fir_decimate_instance_q15 * S,
  1800. q15_t * pSrc,
  1801. q15_t * pDst,
  1802. uint32_t blockSize);
  1803. void csky_fir_decimate_fast_q15(
  1804. const csky_fir_decimate_instance_q15 * S,
  1805. q15_t * pSrc,
  1806. q15_t * pDst,
  1807. uint32_t blockSize);
  1808. csky_status csky_fir_decimate_init_q15(
  1809. csky_fir_decimate_instance_q15 * S,
  1810. uint16_t numTaps,
  1811. uint8_t M,
  1812. q15_t * pCoeffs,
  1813. q15_t * pState,
  1814. uint32_t blockSize);
  1815. void csky_fir_decimate_q31(
  1816. const csky_fir_decimate_instance_q31 * S,
  1817. q31_t * pSrc,
  1818. q31_t * pDst,
  1819. uint32_t blockSize);
  1820. void csky_fir_decimate_fast_q31(
  1821. csky_fir_decimate_instance_q31 * S,
  1822. q31_t * pSrc,
  1823. q31_t * pDst,
  1824. uint32_t blockSize);
  1825. csky_status csky_fir_decimate_init_q31(
  1826. csky_fir_decimate_instance_q31 * S,
  1827. uint16_t numTaps,
  1828. uint8_t M,
  1829. q31_t * pCoeffs,
  1830. q31_t * pState,
  1831. uint32_t blockSize);
  1832. /**
  1833. * @brief Instance structure for the Q15 FIR interpolator.
  1834. */
  1835. typedef struct
  1836. {
  1837. uint8_t L; /**< upsample factor. */
  1838. uint16_t phaseLength; /**< length of each polyphase filter component. */
  1839. q15_t *pCoeffs; /**< points to the coefficient array. The array is of length L*phaseLength. */
  1840. q15_t *pState; /**< points to the state variable array. The array is of length blockSize+phaseLength-1. */
  1841. } csky_fir_interpolate_instance_q15;
  1842. /**
  1843. * @brief Instance structure for the Q31 FIR interpolator.
  1844. */
  1845. typedef struct
  1846. {
  1847. uint8_t L; /**< upsample factor. */
  1848. uint16_t phaseLength; /**< length of each polyphase filter component. */
  1849. q31_t *pCoeffs; /**< points to the coefficient array. The array is of length L*phaseLength. */
  1850. q31_t *pState; /**< points to the state variable array. The array is of length blockSize+phaseLength-1. */
  1851. } csky_fir_interpolate_instance_q31;
  1852. /**
  1853. * @brief Instance structure for the floating-point FIR interpolator.
  1854. */
  1855. typedef struct
  1856. {
  1857. uint8_t L; /**< upsample factor. */
  1858. uint16_t phaseLength; /**< length of each polyphase filter component. */
  1859. float32_t *pCoeffs; /**< points to the coefficient array. The array is of length L*phaseLength. */
  1860. float32_t *pState; /**< points to the state variable array. The array is of length phaseLength+numTaps-1. */
  1861. } csky_fir_interpolate_instance_f32;
  1862. void csky_fir_interpolate_q15(
  1863. const csky_fir_interpolate_instance_q15 * S,
  1864. q15_t * pSrc,
  1865. q15_t * pDst,
  1866. uint32_t blockSize);
  1867. csky_status csky_fir_interpolate_init_q15(
  1868. csky_fir_interpolate_instance_q15 * S,
  1869. uint8_t L,
  1870. uint16_t numTaps,
  1871. q15_t * pCoeffs,
  1872. q15_t * pState,
  1873. uint32_t blockSize);
  1874. void csky_fir_interpolate_q31(
  1875. const csky_fir_interpolate_instance_q31 * S,
  1876. q31_t * pSrc,
  1877. q31_t * pDst,
  1878. uint32_t blockSize);
  1879. csky_status csky_fir_interpolate_init_q31(
  1880. csky_fir_interpolate_instance_q31 * S,
  1881. uint8_t L,
  1882. uint16_t numTaps,
  1883. q31_t * pCoeffs,
  1884. q31_t * pState,
  1885. uint32_t blockSize);
  1886. void csky_fir_interpolate_f32(
  1887. const csky_fir_interpolate_instance_f32 * S,
  1888. float32_t * pSrc,
  1889. float32_t * pDst,
  1890. uint32_t blockSize);
  1891. csky_status csky_fir_interpolate_init_f32(
  1892. csky_fir_interpolate_instance_f32 * S,
  1893. uint8_t L,
  1894. uint16_t numTaps,
  1895. float32_t * pCoeffs,
  1896. float32_t * pState,
  1897. uint32_t blockSize);
  1898. /**
  1899. * @brief Instance structure for the high precision Q31 Biquad cascade filter.
  1900. */
  1901. typedef struct
  1902. {
  1903. uint8_t numStages; /**< number of 2nd order stages in the filter. Overall order is 2*numStages. */
  1904. q63_t *pState; /**< points to the array of state coefficients. The array is of length 4*numStages. */
  1905. q31_t *pCoeffs; /**< points to the array of coefficients. The array is of length 5*numStages. */
  1906. uint8_t postShift; /**< additional shift, in bits, applied to each output sample. */
  1907. } csky_biquad_cas_df1_32x64_ins_q31;
  1908. void csky_biquad_cas_df1_32x64_q31(
  1909. const csky_biquad_cas_df1_32x64_ins_q31 * S,
  1910. q31_t * pSrc,
  1911. q31_t * pDst,
  1912. uint32_t blockSize);
  1913. void csky_biquad_cas_df1_32x64_init_q31(
  1914. csky_biquad_cas_df1_32x64_ins_q31 * S,
  1915. uint8_t numStages,
  1916. q31_t * pCoeffs,
  1917. q63_t * pState,
  1918. uint8_t postShift);
  1919. /**
  1920. * @brief Instance structure for the floating-point transposed direct form II Biquad cascade filter.
  1921. */
  1922. typedef struct
  1923. {
  1924. uint8_t numStages; /**< number of 2nd order stages in the filter. Overall order is 2*numStages. */
  1925. float32_t *pState; /**< points to the array of state coefficients. The array is of length 2*numStages. */
  1926. float32_t *pCoeffs; /**< points to the array of coefficients. The array is of length 5*numStages. */
  1927. } csky_biquad_cascade_df2T_instance_f32;
  1928. /**
  1929. * @brief Instance structure for the floating-point transposed direct form II Biquad cascade filter.
  1930. */
  1931. typedef struct
  1932. {
  1933. uint8_t numStages; /**< number of 2nd order stages in the filter. Overall order is 2*numStages. */
  1934. float32_t *pState; /**< points to the array of state coefficients. The array is of length 4*numStages. */
  1935. float32_t *pCoeffs; /**< points to the array of coefficients. The array is of length 5*numStages. */
  1936. } csky_biquad_cascade_stereo_df2T_instance_f32;
  1937. /**
  1938. * @brief Instance structure for the floating-point transposed direct form II Biquad cascade filter.
  1939. */
  1940. typedef struct
  1941. {
  1942. uint8_t numStages; /**< number of 2nd order stages in the filter. Overall order is 2*numStages. */
  1943. float64_t *pState; /**< points to the array of state coefficients. The array is of length 2*numStages. */
  1944. float64_t *pCoeffs; /**< points to the array of coefficients. The array is of length 5*numStages. */
  1945. } csky_biquad_cascade_df2T_instance_f64;
  1946. void csky_biquad_cascade_df2T_f32(
  1947. const csky_biquad_cascade_df2T_instance_f32 * S,
  1948. float32_t * pSrc,
  1949. float32_t * pDst,
  1950. uint32_t blockSize);
  1951. void csky_biquad_cascade_stereo_df2T_f32(
  1952. const csky_biquad_cascade_stereo_df2T_instance_f32 * S,
  1953. float32_t * pSrc,
  1954. float32_t * pDst,
  1955. uint32_t blockSize);
  1956. void csky_biquad_cascade_df2T_f64(
  1957. const csky_biquad_cascade_df2T_instance_f64 * S,
  1958. float64_t * pSrc,
  1959. float64_t * pDst,
  1960. uint32_t blockSize);
  1961. void csky_biquad_cascade_df2T_init_f32(
  1962. csky_biquad_cascade_df2T_instance_f32 * S,
  1963. uint8_t numStages,
  1964. float32_t * pCoeffs,
  1965. float32_t * pState);
  1966. void csky_biquad_cascade_stereo_df2T_init_f32(
  1967. csky_biquad_cascade_stereo_df2T_instance_f32 * S,
  1968. uint8_t numStages,
  1969. float32_t * pCoeffs,
  1970. float32_t * pState);
  1971. void csky_biquad_cascade_df2T_init_f64(
  1972. csky_biquad_cascade_df2T_instance_f64 * S,
  1973. uint8_t numStages,
  1974. float64_t * pCoeffs,
  1975. float64_t * pState);
  1976. /**
  1977. * @brief Instance structure for the Q15 FIR lattice filter.
  1978. */
  1979. typedef struct
  1980. {
  1981. uint16_t numStages; /**< number of filter stages. */
  1982. q15_t *pState; /**< points to the state variable array. The array is of length numStages. */
  1983. q15_t *pCoeffs; /**< points to the coefficient array. The array is of length numStages. */
  1984. } csky_fir_lattice_instance_q15;
  1985. /**
  1986. * @brief Instance structure for the Q31 FIR lattice filter.
  1987. */
  1988. typedef struct
  1989. {
  1990. uint16_t numStages; /**< number of filter stages. */
  1991. q31_t *pState; /**< points to the state variable array. The array is of length numStages. */
  1992. q31_t *pCoeffs; /**< points to the coefficient array. The array is of length numStages. */
  1993. } csky_fir_lattice_instance_q31;
  1994. /**
  1995. * @brief Instance structure for the floating-point FIR lattice filter.
  1996. */
  1997. typedef struct
  1998. {
  1999. uint16_t numStages; /**< number of filter stages. */
  2000. float32_t *pState; /**< points to the state variable array. The array is of length numStages. */
  2001. float32_t *pCoeffs; /**< points to the coefficient array. The array is of length numStages. */
  2002. } csky_fir_lattice_instance_f32;
  2003. void csky_fir_lattice_init_q15(
  2004. csky_fir_lattice_instance_q15 * S,
  2005. uint16_t numStages,
  2006. q15_t * pCoeffs,
  2007. q15_t * pState);
  2008. void csky_fir_lattice_q15(
  2009. const csky_fir_lattice_instance_q15 * S,
  2010. q15_t * pSrc,
  2011. q15_t * pDst,
  2012. uint32_t blockSize);
  2013. void csky_fir_lattice_init_q31(
  2014. csky_fir_lattice_instance_q31 * S,
  2015. uint16_t numStages,
  2016. q31_t * pCoeffs,
  2017. q31_t * pState);
  2018. void csky_fir_lattice_q31(
  2019. const csky_fir_lattice_instance_q31 * S,
  2020. q31_t * pSrc,
  2021. q31_t * pDst,
  2022. uint32_t blockSize);
  2023. void csky_fir_lattice_init_f32(
  2024. csky_fir_lattice_instance_f32 * S,
  2025. uint16_t numStages,
  2026. float32_t * pCoeffs,
  2027. float32_t * pState);
  2028. void csky_fir_lattice_f32(
  2029. const csky_fir_lattice_instance_f32 * S,
  2030. float32_t * pSrc,
  2031. float32_t * pDst,
  2032. uint32_t blockSize);
  2033. /**
  2034. * @brief Instance structure for the Q15 IIR lattice filter.
  2035. */
  2036. typedef struct
  2037. {
  2038. uint16_t numStages; /**< number of stages in the filter. */
  2039. q15_t *pState; /**< points to the state variable array. The array is of length numStages+blockSize. */
  2040. q15_t *pkCoeffs; /**< points to the reflection coefficient array. The array is of length numStages. */
  2041. q15_t *pvCoeffs; /**< points to the ladder coefficient array. The array is of length numStages+1. */
  2042. } csky_iir_lattice_instance_q15;
  2043. /**
  2044. * @brief Instance structure for the Q31 IIR lattice filter.
  2045. */
  2046. typedef struct
  2047. {
  2048. uint16_t numStages; /**< number of stages in the filter. */
  2049. q31_t *pState; /**< points to the state variable array. The array is of length numStages+blockSize. */
  2050. q31_t *pkCoeffs; /**< points to the reflection coefficient array. The array is of length numStages. */
  2051. q31_t *pvCoeffs; /**< points to the ladder coefficient array. The array is of length numStages+1. */
  2052. } csky_iir_lattice_instance_q31;
  2053. /**
  2054. * @brief Instance structure for the floating-point IIR lattice filter.
  2055. */
  2056. typedef struct
  2057. {
  2058. uint16_t numStages; /**< number of stages in the filter. */
  2059. float32_t *pState; /**< points to the state variable array. The array is of length numStages+blockSize. */
  2060. float32_t *pkCoeffs; /**< points to the reflection coefficient array. The array is of length numStages. */
  2061. float32_t *pvCoeffs; /**< points to the ladder coefficient array. The array is of length numStages+1. */
  2062. } csky_iir_lattice_instance_f32;
  2063. void csky_iir_lattice_f32(
  2064. const csky_iir_lattice_instance_f32 * S,
  2065. float32_t * pSrc,
  2066. float32_t * pDst,
  2067. uint32_t blockSize);
  2068. void csky_iir_lattice_init_f32(
  2069. csky_iir_lattice_instance_f32 * S,
  2070. uint16_t numStages,
  2071. float32_t * pkCoeffs,
  2072. float32_t * pvCoeffs,
  2073. float32_t * pState,
  2074. uint32_t blockSize);
  2075. void csky_iir_lattice_q31(
  2076. const csky_iir_lattice_instance_q31 * S,
  2077. q31_t * pSrc,
  2078. q31_t * pDst,
  2079. uint32_t blockSize);
  2080. void csky_iir_lattice_init_q31(
  2081. csky_iir_lattice_instance_q31 * S,
  2082. uint16_t numStages,
  2083. q31_t * pkCoeffs,
  2084. q31_t * pvCoeffs,
  2085. q31_t * pState,
  2086. uint32_t blockSize);
  2087. void csky_iir_lattice_q15(
  2088. const csky_iir_lattice_instance_q15 * S,
  2089. q15_t * pSrc,
  2090. q15_t * pDst,
  2091. uint32_t blockSize);
  2092. void csky_iir_lattice_init_q15(
  2093. csky_iir_lattice_instance_q15 * S,
  2094. uint16_t numStages,
  2095. q15_t * pkCoeffs,
  2096. q15_t * pvCoeffs,
  2097. q15_t * pState,
  2098. uint32_t blockSize);
  2099. /**
  2100. * @brief Instance structure for the floating-point LMS filter.
  2101. */
  2102. typedef struct
  2103. {
  2104. uint16_t numTaps; /**< number of coefficients in the filter. */
  2105. float32_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  2106. float32_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps. */
  2107. float32_t mu; /**< step size that controls filter coefficient updates. */
  2108. } csky_lms_instance_f32;
  2109. void csky_lms_f32(
  2110. const csky_lms_instance_f32 * S,
  2111. float32_t * pSrc,
  2112. float32_t * pRef,
  2113. float32_t * pOut,
  2114. float32_t * pErr,
  2115. uint32_t blockSize);
  2116. void csky_lms_init_f32(
  2117. csky_lms_instance_f32 * S,
  2118. uint16_t numTaps,
  2119. float32_t * pCoeffs,
  2120. float32_t * pState,
  2121. float32_t mu,
  2122. uint32_t blockSize);
  2123. /**
  2124. * @brief Instance structure for the Q15 LMS filter.
  2125. */
  2126. typedef struct
  2127. {
  2128. uint16_t numTaps; /**< number of coefficients in the filter. */
  2129. q15_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  2130. q15_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps. */
  2131. q15_t mu; /**< step size that controls filter coefficient updates. */
  2132. uint32_t postShift; /**< bit shift applied to coefficients. */
  2133. } csky_lms_instance_q15;
  2134. void csky_lms_init_q15(
  2135. csky_lms_instance_q15 * S,
  2136. uint16_t numTaps,
  2137. q15_t * pCoeffs,
  2138. q15_t * pState,
  2139. q15_t mu,
  2140. uint32_t blockSize,
  2141. uint32_t postShift);
  2142. void csky_lms_q15(
  2143. const csky_lms_instance_q15 * S,
  2144. q15_t * pSrc,
  2145. q15_t * pRef,
  2146. q15_t * pOut,
  2147. q15_t * pErr,
  2148. uint32_t blockSize);
  2149. /**
  2150. * @brief Instance structure for the Q31 LMS filter.
  2151. */
  2152. typedef struct
  2153. {
  2154. uint16_t numTaps; /**< number of coefficients in the filter. */
  2155. q31_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  2156. q31_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps. */
  2157. q31_t mu; /**< step size that controls filter coefficient updates. */
  2158. uint32_t postShift; /**< bit shift applied to coefficients. */
  2159. } csky_lms_instance_q31;
  2160. void csky_lms_q31(
  2161. const csky_lms_instance_q31 * S,
  2162. q31_t * pSrc,
  2163. q31_t * pRef,
  2164. q31_t * pOut,
  2165. q31_t * pErr,
  2166. uint32_t blockSize);
  2167. void csky_lms_init_q31(
  2168. csky_lms_instance_q31 * S,
  2169. uint16_t numTaps,
  2170. q31_t * pCoeffs,
  2171. q31_t * pState,
  2172. q31_t mu,
  2173. uint32_t blockSize,
  2174. uint32_t postShift);
  2175. /**
  2176. * @brief Instance structure for the floating-point normalized LMS filter.
  2177. */
  2178. typedef struct
  2179. {
  2180. uint16_t numTaps; /**< number of coefficients in the filter. */
  2181. float32_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  2182. float32_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps. */
  2183. float32_t mu; /**< step size that control filter coefficient updates. */
  2184. float32_t energy; /**< saves previous frame energy. */
  2185. float32_t x0; /**< saves previous input sample. */
  2186. } csky_lms_norm_instance_f32;
  2187. void csky_lms_norm_f32(
  2188. csky_lms_norm_instance_f32 * S,
  2189. float32_t * pSrc,
  2190. float32_t * pRef,
  2191. float32_t * pOut,
  2192. float32_t * pErr,
  2193. uint32_t blockSize);
  2194. void csky_lms_norm_init_f32(
  2195. csky_lms_norm_instance_f32 * S,
  2196. uint16_t numTaps,
  2197. float32_t * pCoeffs,
  2198. float32_t * pState,
  2199. float32_t mu,
  2200. uint32_t blockSize);
  2201. /**
  2202. * @brief Instance structure for the Q31 normalized LMS filter.
  2203. */
  2204. typedef struct
  2205. {
  2206. uint16_t numTaps; /**< number of coefficients in the filter. */
  2207. q31_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  2208. q31_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps. */
  2209. q31_t mu; /**< step size that controls filter coefficient updates. */
  2210. uint8_t postShift; /**< bit shift applied to coefficients. */
  2211. q31_t *recipTable; /**< points to the reciprocal initial value table. */
  2212. q31_t energy; /**< saves previous frame energy. */
  2213. q31_t x0; /**< saves previous input sample. */
  2214. } csky_lms_norm_instance_q31;
  2215. void csky_lms_norm_q31(
  2216. csky_lms_norm_instance_q31 * S,
  2217. q31_t * pSrc,
  2218. q31_t * pRef,
  2219. q31_t * pOut,
  2220. q31_t * pErr,
  2221. uint32_t blockSize);
  2222. void csky_lms_norm_init_q31(
  2223. csky_lms_norm_instance_q31 * S,
  2224. uint16_t numTaps,
  2225. q31_t * pCoeffs,
  2226. q31_t * pState,
  2227. q31_t mu,
  2228. uint32_t blockSize,
  2229. uint8_t postShift);
  2230. /**
  2231. * @brief Instance structure for the Q15 normalized LMS filter.
  2232. */
  2233. typedef struct
  2234. {
  2235. uint16_t numTaps; /**< Number of coefficients in the filter. */
  2236. q15_t *pState; /**< points to the state variable array. The array is of length numTaps+blockSize-1. */
  2237. q15_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps. */
  2238. q15_t mu; /**< step size that controls filter coefficient updates. */
  2239. uint8_t postShift; /**< bit shift applied to coefficients. */
  2240. q15_t *recipTable; /**< Points to the reciprocal initial value table. */
  2241. q15_t energy; /**< saves previous frame energy. */
  2242. q15_t x0; /**< saves previous input sample. */
  2243. } csky_lms_norm_instance_q15;
  2244. void csky_lms_norm_q15(
  2245. csky_lms_norm_instance_q15 * S,
  2246. q15_t * pSrc,
  2247. q15_t * pRef,
  2248. q15_t * pOut,
  2249. q15_t * pErr,
  2250. uint32_t blockSize);
  2251. void csky_lms_norm_init_q15(
  2252. csky_lms_norm_instance_q15 * S,
  2253. uint16_t numTaps,
  2254. q15_t * pCoeffs,
  2255. q15_t * pState,
  2256. q15_t mu,
  2257. uint32_t blockSize,
  2258. uint8_t postShift);
  2259. void csky_correlate_f32(
  2260. float32_t * pSrcA,
  2261. uint32_t srcALen,
  2262. float32_t * pSrcB,
  2263. uint32_t srcBLen,
  2264. float32_t * pDst);
  2265. void csky_correlate_opt_q15(
  2266. q15_t * pSrcA,
  2267. uint32_t srcALen,
  2268. q15_t * pSrcB,
  2269. uint32_t srcBLen,
  2270. q15_t * pDst,
  2271. q15_t * pScratch);
  2272. void csky_correlate_q15(
  2273. q15_t * pSrcA,
  2274. uint32_t srcALen,
  2275. q15_t * pSrcB,
  2276. uint32_t srcBLen,
  2277. q15_t * pDst);
  2278. void csky_correlate_fast_q15(
  2279. q15_t * pSrcA,
  2280. uint32_t srcALen,
  2281. q15_t * pSrcB,
  2282. uint32_t srcBLen,
  2283. q15_t * pDst);
  2284. void csky_correlate_fast_opt_q15(
  2285. q15_t * pSrcA,
  2286. uint32_t srcALen,
  2287. q15_t * pSrcB,
  2288. uint32_t srcBLen,
  2289. q15_t * pDst,
  2290. q15_t * pScratch);
  2291. void csky_correlate_q31(
  2292. q31_t * pSrcA,
  2293. uint32_t srcALen,
  2294. q31_t * pSrcB,
  2295. uint32_t srcBLen,
  2296. q31_t * pDst);
  2297. void csky_correlate_fast_q31(
  2298. q31_t * pSrcA,
  2299. uint32_t srcALen,
  2300. q31_t * pSrcB,
  2301. uint32_t srcBLen,
  2302. q31_t * pDst);
  2303. void csky_correlate_opt_q7(
  2304. q7_t * pSrcA,
  2305. uint32_t srcALen,
  2306. q7_t * pSrcB,
  2307. uint32_t srcBLen,
  2308. q7_t * pDst,
  2309. q15_t * pScratch1,
  2310. q15_t * pScratch2);
  2311. void csky_correlate_q7(
  2312. q7_t * pSrcA,
  2313. uint32_t srcALen,
  2314. q7_t * pSrcB,
  2315. uint32_t srcBLen,
  2316. q7_t * pDst);
  2317. /**
  2318. * @brief Instance structure for the floating-point sparse FIR filter.
  2319. */
  2320. typedef struct
  2321. {
  2322. uint16_t numTaps; /**< number of coefficients in the filter. */
  2323. uint16_t stateIndex; /**< state buffer index. Points to the oldest sample in the state buffer. */
  2324. float32_t *pState; /**< points to the state buffer array. The array is of length maxDelay+blockSize-1. */
  2325. float32_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps.*/
  2326. uint16_t maxDelay; /**< maximum offset specified by the pTapDelay array. */
  2327. int32_t *pTapDelay; /**< points to the array of delay values. The array is of length numTaps. */
  2328. } csky_fir_sparse_instance_f32;
  2329. /**
  2330. * @brief Instance structure for the Q31 sparse FIR filter.
  2331. */
  2332. typedef struct
  2333. {
  2334. uint16_t numTaps; /**< number of coefficients in the filter. */
  2335. uint16_t stateIndex; /**< state buffer index. Points to the oldest sample in the state buffer. */
  2336. q31_t *pState; /**< points to the state buffer array. The array is of length maxDelay+blockSize-1. */
  2337. q31_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps.*/
  2338. uint16_t maxDelay; /**< maximum offset specified by the pTapDelay array. */
  2339. int32_t *pTapDelay; /**< points to the array of delay values. The array is of length numTaps. */
  2340. } csky_fir_sparse_instance_q31;
  2341. /**
  2342. * @brief Instance structure for the Q15 sparse FIR filter.
  2343. */
  2344. typedef struct
  2345. {
  2346. uint16_t numTaps; /**< number of coefficients in the filter. */
  2347. uint16_t stateIndex; /**< state buffer index. Points to the oldest sample in the state buffer. */
  2348. q15_t *pState; /**< points to the state buffer array. The array is of length maxDelay+blockSize-1. */
  2349. q15_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps.*/
  2350. uint16_t maxDelay; /**< maximum offset specified by the pTapDelay array. */
  2351. int32_t *pTapDelay; /**< points to the array of delay values. The array is of length numTaps. */
  2352. } csky_fir_sparse_instance_q15;
  2353. /**
  2354. * @brief Instance structure for the Q7 sparse FIR filter.
  2355. */
  2356. typedef struct
  2357. {
  2358. uint16_t numTaps; /**< number of coefficients in the filter. */
  2359. uint16_t stateIndex; /**< state buffer index. Points to the oldest sample in the state buffer. */
  2360. q7_t *pState; /**< points to the state buffer array. The array is of length maxDelay+blockSize-1. */
  2361. q7_t *pCoeffs; /**< points to the coefficient array. The array is of length numTaps.*/
  2362. uint16_t maxDelay; /**< maximum offset specified by the pTapDelay array. */
  2363. int32_t *pTapDelay; /**< points to the array of delay values. The array is of length numTaps. */
  2364. } csky_fir_sparse_instance_q7;
  2365. void csky_fir_sparse_f32(
  2366. csky_fir_sparse_instance_f32 * S,
  2367. float32_t * pSrc,
  2368. float32_t * pDst,
  2369. float32_t * pScratchIn,
  2370. uint32_t blockSize);
  2371. void csky_fir_sparse_init_f32(
  2372. csky_fir_sparse_instance_f32 * S,
  2373. uint16_t numTaps,
  2374. float32_t * pCoeffs,
  2375. float32_t * pState,
  2376. int32_t * pTapDelay,
  2377. uint16_t maxDelay,
  2378. uint32_t blockSize);
  2379. void csky_fir_sparse_q31(
  2380. csky_fir_sparse_instance_q31 * S,
  2381. q31_t * pSrc,
  2382. q31_t * pDst,
  2383. q31_t * pScratchIn,
  2384. uint32_t blockSize);
  2385. void csky_fir_sparse_init_q31(
  2386. csky_fir_sparse_instance_q31 * S,
  2387. uint16_t numTaps,
  2388. q31_t * pCoeffs,
  2389. q31_t * pState,
  2390. int32_t * pTapDelay,
  2391. uint16_t maxDelay,
  2392. uint32_t blockSize);
  2393. void csky_fir_sparse_q15(
  2394. csky_fir_sparse_instance_q15 * S,
  2395. q15_t * pSrc,
  2396. q15_t * pDst,
  2397. q15_t * pScratchIn,
  2398. q31_t * pScratchOut,
  2399. uint32_t blockSize);
  2400. void csky_fir_sparse_init_q15(
  2401. csky_fir_sparse_instance_q15 * S,
  2402. uint16_t numTaps,
  2403. q15_t * pCoeffs,
  2404. q15_t * pState,
  2405. int32_t * pTapDelay,
  2406. uint16_t maxDelay,
  2407. uint32_t blockSize);
  2408. void csky_fir_sparse_q7(
  2409. csky_fir_sparse_instance_q7 * S,
  2410. q7_t * pSrc,
  2411. q7_t * pDst,
  2412. q7_t * pScratchIn,
  2413. q31_t * pScratchOut,
  2414. uint32_t blockSize);
  2415. void csky_fir_sparse_init_q7(
  2416. csky_fir_sparse_instance_q7 * S,
  2417. uint16_t numTaps,
  2418. q7_t * pCoeffs,
  2419. q7_t * pState,
  2420. int32_t * pTapDelay,
  2421. uint16_t maxDelay,
  2422. uint32_t blockSize);
  2423. void csky_sin_cos_f32(
  2424. float32_t theta,
  2425. float32_t * pSinVal,
  2426. float32_t * pCosVal);
  2427. void csky_sin_cos_q31(
  2428. q31_t theta,
  2429. q31_t * pSinVal,
  2430. q31_t * pCosVal);
  2431. void csky_cmplx_conj_f32(
  2432. float32_t * pSrc,
  2433. float32_t * pDst,
  2434. uint32_t numSamples);
  2435. void csky_cmplx_conj_q31(
  2436. q31_t * pSrc,
  2437. q31_t * pDst,
  2438. uint32_t numSamples);
  2439. void csky_cmplx_conj_q15(
  2440. q15_t * pSrc,
  2441. q15_t * pDst,
  2442. uint32_t numSamples);
  2443. void csky_cmplx_mag_squared_f32(
  2444. float32_t * pSrc,
  2445. float32_t * pDst,
  2446. uint32_t numSamples);
  2447. void csky_cmplx_mag_squared_q31(
  2448. q31_t * pSrc,
  2449. q31_t * pDst,
  2450. uint32_t numSamples);
  2451. void csky_cmplx_mag_squared_q15(
  2452. q15_t * pSrc,
  2453. q15_t * pDst,
  2454. uint32_t numSamples);
  2455. /**
  2456. * @ingroup groupController
  2457. */
  2458. /**
  2459. * @defgroup PID PID Motor Control
  2460. *
  2461. * A Proportional Integral Derivative (PID) controller is a generic feedback control
  2462. * loop mechanism widely used in industrial control systems.
  2463. * A PID controller is the most commonly used type of feedback controller.
  2464. *
  2465. * This set of functions implements (PID) controllers
  2466. * for Q15, Q31, and floating-point data types. The functions operate on a single sample
  2467. * of data and each call to the function returns a single processed value.
  2468. * <code>S</code> points to an instance of the PID control data structure. <code>in</code>
  2469. * is the input sample value. The functions return the output value.
  2470. *
  2471. * \par Algorithm:
  2472. * <pre>
  2473. * y[n] = y[n-1] + A0 * x[n] + A1 * x[n-1] + A2 * x[n-2]
  2474. * A0 = Kp + Ki + Kd
  2475. * A1 = (-Kp ) - (2 * Kd )
  2476. * A2 = Kd </pre>
  2477. *
  2478. * \par
  2479. * where \c Kp is proportional constant, \c Ki is Integral constant and \c Kd is Derivative constant
  2480. *
  2481. * \par
  2482. * \image html PID.gif "Proportional Integral Derivative Controller"
  2483. *
  2484. * \par
  2485. * The PID controller calculates an "error" value as the difference between
  2486. * the measured output and the reference input.
  2487. * The controller attempts to minimize the error by adjusting the process control inputs.
  2488. * The proportional value determines the reaction to the current error,
  2489. * the integral value determines the reaction based on the sum of recent errors,
  2490. * and the derivative value determines the reaction based on the rate at which the error has been changing.
  2491. *
  2492. * \par Instance Structure
  2493. * The Gains A0, A1, A2 and state variables for a PID controller are stored together in an instance data structure.
  2494. * A separate instance structure must be defined for each PID Controller.
  2495. * There are separate instance structure declarations for each of the 3 supported data types.
  2496. *
  2497. * \par Reset Functions
  2498. * There is also an associated reset function for each data type which clears the state array.
  2499. *
  2500. * \par Initialization Functions
  2501. * There is also an associated initialization function for each data type.
  2502. * The initialization function performs the following operations:
  2503. * - Initializes the Gains A0, A1, A2 from Kp,Ki, Kd gains.
  2504. * - Zeros out the values in the state buffer.
  2505. *
  2506. * \par
  2507. * Instance structure cannot be placed into a const data section and it is recommended to use the initialization function.
  2508. *
  2509. * \par Fixed-Point Behavior
  2510. * Care must be taken when using the fixed-point versions of the PID Controller functions.
  2511. * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
  2512. * Refer to the function specific documentation below for usage guidelines.
  2513. */
  2514. /**
  2515. * @addtogroup PID
  2516. * @{
  2517. */
  2518. /**
  2519. * @brief Process function for the floating-point PID Control.
  2520. * @param[in,out] S is an instance of the floating-point PID Control structure
  2521. * @param[in] in input sample to process
  2522. * @return out processed output sample.
  2523. */
  2524. __STATIC_INLINE float32_t csky_pid_f32(
  2525. csky_pid_instance_f32 * S,
  2526. float32_t in)
  2527. {
  2528. float32_t out;
  2529. /* y[n] = y[n-1] + A0 * x[n] + A1 * x[n-1] + A2 * x[n-2] */
  2530. out = (S->A0 * in) +
  2531. (S->A1 * S->state[0]) + (S->A2 * S->state[1]) + (S->state[2]);
  2532. /* Update state */
  2533. S->state[1] = S->state[0];
  2534. S->state[0] = in;
  2535. S->state[2] = out;
  2536. /* return to application */
  2537. return (out);
  2538. }
  2539. /**
  2540. * @}
  2541. */ // end of PID group
  2542. /**
  2543. * @addtogroup PID
  2544. * @{
  2545. */
  2546. /**
  2547. * @brief Process function for the Q31 PID Control.
  2548. * @param[in,out] S points to an instance of the Q31 PID Control structure
  2549. * @param[in] in input sample to process
  2550. * @return out processed output sample.
  2551. *
  2552. * <b>Scaling and Overflow Behavior:</b>
  2553. * \par
  2554. * The function is implemented using an internal 64-bit accumulator.
  2555. * The accumulator has a 2.62 format and maintains full precision of the intermediate multiplication results but provides only a single guard bit.
  2556. * Thus, if the accumulator result overflows it wraps around rather than clip.
  2557. * In order to avoid overflows completely the input signal must be scaled down by 2 bits as there are four additions.
  2558. * After all multiply-accumulates are performed, the 2.62 accumulator is truncated to 1.32 format and then saturated to 1.31 format.
  2559. */
  2560. __STATIC_INLINE q31_t csky_pid_q31(
  2561. csky_pid_instance_q31 * S,
  2562. q31_t in)
  2563. {
  2564. q63_t acc;
  2565. q31_t out;
  2566. #ifdef CSKY_SIMD
  2567. /* acc = A0 * x[n] */
  2568. acc = mult_32x32_keep64(S->A0, in);
  2569. /* acc += A1 * x[n-1] */
  2570. acc = multAcc_32x32_keep64(acc, S->A1, S->state[0]);
  2571. /* acc += A2 * x[n-2] */
  2572. acc = multAcc_32x32_keep64(acc, S->A2, S->state[1]);
  2573. /* convert output to 1.31 format to add y[n-1] */
  2574. out = dext_31(acc);
  2575. #else
  2576. /* acc = A0 * x[n] */
  2577. acc = (q63_t) S->A0 * in;
  2578. /* acc += A1 * x[n-1] */
  2579. acc += (q63_t) S->A1 * S->state[0];
  2580. /* acc += A2 * x[n-2] */
  2581. acc += (q63_t) S->A2 * S->state[1];
  2582. /* convert output to 1.31 format to add y[n-1] */
  2583. out = (q31_t) (acc >> 31u);
  2584. #endif
  2585. /* out += y[n-1] */
  2586. out += S->state[2];
  2587. /* Update state */
  2588. S->state[1] = S->state[0];
  2589. S->state[0] = in;
  2590. S->state[2] = out;
  2591. /* return to application */
  2592. return (out);
  2593. }
  2594. /**
  2595. * @}
  2596. */ // end of PID group
  2597. /**
  2598. * @addtogroup PID
  2599. * @{
  2600. */
  2601. /**
  2602. * @brief Process function for the Q15 PID Control.
  2603. * @param[in,out] S points to an instance of the Q15 PID Control structure
  2604. * @param[in] in input sample to process
  2605. * @return out processed output sample.
  2606. *
  2607. * <b>Scaling and Overflow Behavior:</b>
  2608. * \par
  2609. * The function is implemented using a 64-bit internal accumulator.
  2610. * Both Gains and state variables are represented in 1.15 format and multiplications yield a 2.30 result.
  2611. * The 2.30 intermediate results are accumulated in a 64-bit accumulator in 34.30 format.
  2612. * There is no risk of internal overflow with this approach and the full precision of intermediate multiplications is preserved.
  2613. * After all additions have been performed, the accumulator is truncated to 34.15 format by discarding low 15 bits.
  2614. * Lastly, the accumulator is saturated to yield a result in 1.15 format.
  2615. */
  2616. __STATIC_INLINE q15_t csky_pid_q15(
  2617. csky_pid_instance_q15 * S,
  2618. q15_t in)
  2619. {
  2620. q63_t acc;
  2621. q15_t out;
  2622. /* acc = A0 * x[n] */
  2623. acc = ((q31_t) S->A0) * in;
  2624. /* acc += A1 * x[n-1] + A2 * x[n-2] */
  2625. acc += (q31_t) S->A1 * S->state[0];
  2626. acc += (q31_t) S->A2 * S->state[1];
  2627. /* acc += y[n-1] */
  2628. acc += (q31_t) S->state[2] << 15;
  2629. /* saturate the output */
  2630. out = (q15_t) (__SSAT_16((acc >> 15)));
  2631. /* Update state */
  2632. S->state[1] = S->state[0];
  2633. S->state[0] = in;
  2634. S->state[2] = out;
  2635. /* return to application */
  2636. return (out);
  2637. }
  2638. /**
  2639. * @}
  2640. */ // end of PID group
  2641. csky_status csky_mat_inverse_f32(
  2642. const csky_matrix_instance_f32 * src,
  2643. csky_matrix_instance_f32 * dst);
  2644. csky_status csky_mat_inverse_f64(
  2645. const csky_matrix_instance_f64 * src,
  2646. csky_matrix_instance_f64 * dst);
  2647. /**
  2648. * @ingroup groupController
  2649. */
  2650. /**
  2651. * @defgroup clarke Vector Clarke Transform
  2652. * Forward Clarke transform converts the instantaneous stator phases into a two-coordinate time invariant vector.
  2653. * Generally the Clarke transform uses three-phase currents <code>Ia, Ib and Ic</code> to calculate currents
  2654. * in the two-phase orthogonal stator axis <code>Ialpha</code> and <code>Ibeta</code>.
  2655. * When <code>Ialpha</code> is superposed with <code>Ia</code> as shown in the figure below
  2656. * \image html clarke.gif Stator current space vector and its components in (a,b).
  2657. * and <code>Ia + Ib + Ic = 0</code>, in this condition <code>Ialpha</code> and <code>Ibeta</code>
  2658. * can be calculated using only <code>Ia</code> and <code>Ib</code>.
  2659. *
  2660. * The function operates on a single sample of data and each call to the function returns the processed output.
  2661. * The library provides separate functions for Q31 and floating-point data types.
  2662. * \par Algorithm
  2663. * \image html clarkeFormula.gif
  2664. * where <code>Ia</code> and <code>Ib</code> are the instantaneous stator phases and
  2665. * <code>pIalpha</code> and <code>pIbeta</code> are the two coordinates of time invariant vector.
  2666. * \par Fixed-Point Behavior
  2667. * Care must be taken when using the Q31 version of the Clarke transform.
  2668. * In particular, the overflow and saturation behavior of the accumulator used must be considered.
  2669. * Refer to the function specific documentation below for usage guidelines.
  2670. */
  2671. /**
  2672. * @addtogroup clarke
  2673. * @{
  2674. */
  2675. /**
  2676. *
  2677. * @brief Floating-point Clarke transform
  2678. * @param[in] Ia input three-phase coordinate a
  2679. * @param[in] Ib input three-phase coordinate b
  2680. * @param[out] pIalpha points to output two-phase orthogonal vector axis alpha
  2681. * @param[out] pIbeta points to output two-phase orthogonal vector axis beta
  2682. */
  2683. __STATIC_INLINE void csky_clarke_f32(
  2684. float32_t Ia,
  2685. float32_t Ib,
  2686. float32_t * pIalpha,
  2687. float32_t * pIbeta)
  2688. {
  2689. /* Calculate pIalpha using the equation, pIalpha = Ia */
  2690. *pIalpha = Ia;
  2691. /* Calculate pIbeta using the equation, pIbeta = (1/sqrt(3)) * Ia + (2/sqrt(3)) * Ib */
  2692. *pIbeta = ((float32_t) 0.57735026919 * Ia + (float32_t) 1.15470053838 * Ib);
  2693. }
  2694. /**
  2695. * @}
  2696. */ // end of clarke group
  2697. /**
  2698. * @addtogroup clarke
  2699. * @{
  2700. */
  2701. /**
  2702. * @brief Clarke transform for Q31 version
  2703. * @param[in] Ia input three-phase coordinate a
  2704. * @param[in] Ib input three-phase coordinate b
  2705. * @param[out] pIalpha points to output two-phase orthogonal vector axis alpha
  2706. * @param[out] pIbeta points to output two-phase orthogonal vector axis beta
  2707. *
  2708. * <b>Scaling and Overflow Behavior:</b>
  2709. * \par
  2710. * The function is implemented using an internal 32-bit accumulator.
  2711. * The accumulator maintains 1.31 format by truncating lower 31 bits of the intermediate multiplication in 2.62 format.
  2712. * There is saturation on the addition, hence there is no risk of overflow.
  2713. */
  2714. __STATIC_INLINE void csky_clarke_q31(
  2715. q31_t Ia,
  2716. q31_t Ib,
  2717. q31_t * pIalpha,
  2718. q31_t * pIbeta)
  2719. {
  2720. q31_t product1, product2; /* Temporary variables used to store intermediate results */
  2721. /* Calculating pIalpha from Ia by equation pIalpha = Ia */
  2722. *pIalpha = Ia;
  2723. #ifdef CSKY_SIMD
  2724. /* Intermediate product is calculated by (1/(sqrt(3)) * Ia) */
  2725. product1 = mult_32x32_dext_30(Ia, 0x24F34E8B);
  2726. /* Intermediate product is calculated by (2/sqrt(3) * Ib) */
  2727. product2 = mult_32x32_dext_30(Ib, 0x49E69D16);
  2728. #else
  2729. /* Intermediate product is calculated by (1/(sqrt(3)) * Ia) */
  2730. product1 = (q31_t) (((q63_t) Ia * 0x24F34E8B) >> 30);
  2731. /* Intermediate product is calculated by (2/sqrt(3) * Ib) */
  2732. product2 = (q31_t) (((q63_t) Ib * 0x49E69D16) >> 30);
  2733. #endif
  2734. /* pIbeta is calculated by adding the intermediate products */
  2735. *pIbeta = __QADD(product1, product2);
  2736. }
  2737. /**
  2738. * @}
  2739. */ // end of clarke group
  2740. void csky_q7_to_q31(
  2741. q7_t * pSrc,
  2742. q31_t * pDst,
  2743. uint32_t blockSize);
  2744. /**
  2745. * @ingroup groupController
  2746. */
  2747. /**
  2748. * @defgroup inv_clarke Vector Inverse Clarke Transform
  2749. * Inverse Clarke transform converts the two-coordinate time invariant vector into instantaneous stator phases.
  2750. *
  2751. * The function operates on a single sample of data and each call to the function returns the processed output.
  2752. * The library provides separate functions for Q31 and floating-point data types.
  2753. * \par Algorithm
  2754. * \image html clarkeInvFormula.gif
  2755. * where <code>pIa</code> and <code>pIb</code> are the instantaneous stator phases and
  2756. * <code>Ialpha</code> and <code>Ibeta</code> are the two coordinates of time invariant vector.
  2757. * \par Fixed-Point Behavior
  2758. * Care must be taken when using the Q31 version of the Clarke transform.
  2759. * In particular, the overflow and saturation behavior of the accumulator used must be considered.
  2760. * Refer to the function specific documentation below for usage guidelines.
  2761. */
  2762. /**
  2763. * @addtogroup inv_clarke
  2764. * @{
  2765. */
  2766. /**
  2767. * @brief Floating-point Inverse Clarke transform
  2768. * @param[in] Ialpha input two-phase orthogonal vector axis alpha
  2769. * @param[in] Ibeta input two-phase orthogonal vector axis beta
  2770. * @param[out] pIa points to output three-phase coordinate <code>a</code>
  2771. * @param[out] pIb points to output three-phase coordinate <code>b</code>
  2772. */
  2773. __STATIC_INLINE void csky_inv_clarke_f32(
  2774. float32_t Ialpha,
  2775. float32_t Ibeta,
  2776. float32_t * pIa,
  2777. float32_t * pIb)
  2778. {
  2779. /* Calculating pIa from Ialpha by equation pIa = Ialpha */
  2780. *pIa = Ialpha;
  2781. /* Calculating pIb from Ialpha and Ibeta by equation pIb = -(1/2) * Ialpha + (sqrt(3)/2) * Ibeta */
  2782. *pIb = -0.5f * Ialpha + 0.8660254039f * Ibeta;
  2783. }
  2784. /**
  2785. * @}
  2786. */ // end of inv_clarke group
  2787. /**
  2788. * @addtogroup inv_clarke
  2789. * @{
  2790. */
  2791. /**
  2792. * @brief Inverse Clarke transform for Q31 version
  2793. * @param[in] Ialpha input two-phase orthogonal vector axis alpha
  2794. * @param[in] Ibeta input two-phase orthogonal vector axis beta
  2795. * @param[out] pIa points to output three-phase coordinate <code>a</code>
  2796. * @param[out] pIb points to output three-phase coordinate <code>b</code>
  2797. *
  2798. * <b>Scaling and Overflow Behavior:</b>
  2799. * \par
  2800. * The function is implemented using an internal 32-bit accumulator.
  2801. * The accumulator maintains 1.31 format by truncating lower 31 bits of the intermediate multiplication in 2.62 format.
  2802. * There is saturation on the subtraction, hence there is no risk of overflow.
  2803. */
  2804. __STATIC_INLINE void csky_inv_clarke_q31(
  2805. q31_t Ialpha,
  2806. q31_t Ibeta,
  2807. q31_t * pIa,
  2808. q31_t * pIb)
  2809. {
  2810. q31_t product1, product2; /* Temporary variables used to store intermediate results */
  2811. /* Calculating pIa from Ialpha by equation pIa = Ialpha */
  2812. *pIa = Ialpha;
  2813. #ifdef CSKY_SIMD
  2814. /* Intermediate product is calculated by (1/(2*sqrt(3)) * Ia) */
  2815. product1 = mult_32x32_dext_31(Ialpha, 0x40000000);
  2816. /* Intermediate product is calculated by (1/sqrt(3) * pIb) */
  2817. product2 = mult_32x32_dext_31(Ibeta, 0x6ED9EBA1);
  2818. #else
  2819. /* Intermediate product is calculated by (1/(2*sqrt(3)) * Ia) */
  2820. product1 = (q31_t) (((q63_t) (Ialpha) * (0x40000000)) >> 31);
  2821. /* Intermediate product is calculated by (1/sqrt(3) * pIb) */
  2822. product2 = (q31_t) (((q63_t) (Ibeta) * (0x6ED9EBA1)) >> 31);
  2823. #endif
  2824. /* pIb is calculated by subtracting the products */
  2825. *pIb = __QSUB(product2, product1);
  2826. }
  2827. /**
  2828. * @}
  2829. */ // end of inv_clarke group
  2830. void csky_q7_to_q15(
  2831. q7_t * pSrc,
  2832. q15_t * pDst,
  2833. uint32_t blockSize);
  2834. /**
  2835. * @ingroup groupController
  2836. */
  2837. /**
  2838. * @defgroup park Vector Park Transform
  2839. *
  2840. * Forward Park transform converts the input two-coordinate vector to flux and torque components.
  2841. * The Park transform can be used to realize the transformation of the <code>Ialpha</code> and the <code>Ibeta</code> currents
  2842. * from the stationary to the moving reference frame and control the spatial relationship between
  2843. * the stator vector current and rotor flux vector.
  2844. * If we consider the d axis aligned with the rotor flux, the diagram below shows the
  2845. * current vector and the relationship from the two reference frames:
  2846. * \image html park.gif "Stator current space vector and its component in (a,b) and in the d,q rotating reference frame"
  2847. *
  2848. * The function operates on a single sample of data and each call to the function returns the processed output.
  2849. * The library provides separate functions for Q31 and floating-point data types.
  2850. * \par Algorithm
  2851. * \image html parkFormula.gif
  2852. * where <code>Ialpha</code> and <code>Ibeta</code> are the stator vector components,
  2853. * <code>pId</code> and <code>pIq</code> are rotor vector components and <code>cosVal</code> and <code>sinVal</code> are the
  2854. * cosine and sine values of theta (rotor flux position).
  2855. * \par Fixed-Point Behavior
  2856. * Care must be taken when using the Q31 version of the Park transform.
  2857. * In particular, the overflow and saturation behavior of the accumulator used must be considered.
  2858. * Refer to the function specific documentation below for usage guidelines.
  2859. */
  2860. /**
  2861. * @addtogroup park
  2862. * @{
  2863. */
  2864. /**
  2865. * @brief Floating-point Park transform
  2866. * @param[in] Ialpha input two-phase vector coordinate alpha
  2867. * @param[in] Ibeta input two-phase vector coordinate beta
  2868. * @param[out] pId points to output rotor reference frame d
  2869. * @param[out] pIq points to output rotor reference frame q
  2870. * @param[in] sinVal sine value of rotation angle theta
  2871. * @param[in] cosVal cosine value of rotation angle theta
  2872. *
  2873. * The function implements the forward Park transform.
  2874. *
  2875. */
  2876. __STATIC_INLINE void csky_park_f32(
  2877. float32_t Ialpha,
  2878. float32_t Ibeta,
  2879. float32_t * pId,
  2880. float32_t * pIq,
  2881. float32_t sinVal,
  2882. float32_t cosVal)
  2883. {
  2884. /* Calculate pId using the equation, pId = Ialpha * cosVal + Ibeta * sinVal */
  2885. *pId = Ialpha * cosVal + Ibeta * sinVal;
  2886. /* Calculate pIq using the equation, pIq = - Ialpha * sinVal + Ibeta * cosVal */
  2887. *pIq = -Ialpha * sinVal + Ibeta * cosVal;
  2888. }
  2889. /**
  2890. * @}
  2891. */ // end of park group
  2892. /**
  2893. * @addtogroup park
  2894. * @{
  2895. */
  2896. /**
  2897. * @brief Park transform for Q31 version
  2898. * @param[in] Ialpha input two-phase vector coordinate alpha
  2899. * @param[in] Ibeta input two-phase vector coordinate beta
  2900. * @param[out] pId points to output rotor reference frame d
  2901. * @param[out] pIq points to output rotor reference frame q
  2902. * @param[in] sinVal sine value of rotation angle theta
  2903. * @param[in] cosVal cosine value of rotation angle theta
  2904. *
  2905. * <b>Scaling and Overflow Behavior:</b>
  2906. * \par
  2907. * The function is implemented using an internal 32-bit accumulator.
  2908. * The accumulator maintains 1.31 format by truncating lower 31 bits of the intermediate multiplication in 2.62 format.
  2909. * There is saturation on the addition and subtraction, hence there is no risk of overflow.
  2910. */
  2911. __STATIC_INLINE void csky_park_q31(
  2912. q31_t Ialpha,
  2913. q31_t Ibeta,
  2914. q31_t * pId,
  2915. q31_t * pIq,
  2916. q31_t sinVal,
  2917. q31_t cosVal)
  2918. {
  2919. #ifdef CSKY_SIMD
  2920. __ASM volatile(
  2921. "rmul.s32.h t0, %0, %3\n\t"
  2922. "rmul.s32.h t1, %1, %2\n\t"
  2923. "add.s32.s t0, t0, t1\n\t"
  2924. "st.w t0, (%4, 0x0)\n\t"
  2925. "rmul.s32.h t0, %0, %2\n\t"
  2926. "rmul.s32.h t1, %1, %3\n\t"
  2927. "sub.s32.s t1, t1, t0\n\t"
  2928. "st.w t1, (%5, 0x0)\n\t"
  2929. ::"r"(Ialpha),"r"(Ibeta),"r"(sinVal),"r"(cosVal),"r"(pId),"r"(pIq)
  2930. :"t0","t1", "memory");
  2931. #else
  2932. q31_t product1, product2; /* Temporary variables used to store intermediate results */
  2933. q31_t product3, product4; /* Temporary variables used to store intermediate results */
  2934. /* Intermediate product is calculated by (Ialpha * cosVal) */
  2935. product1 = clip_q63_to_q31 (((q63_t) (Ialpha) * (cosVal)) >> 31);
  2936. /* Intermediate product is calculated by (Ibeta * sinVal) */
  2937. product2 = clip_q63_to_q31 (((q63_t) (Ibeta) * (sinVal)) >> 31);
  2938. /* Intermediate product is calculated by (Ialpha * sinVal) */
  2939. product3 = clip_q63_to_q31 (((q63_t) (Ialpha) * (sinVal)) >> 31);
  2940. /* Intermediate product is calculated by (Ibeta * cosVal) */
  2941. product4 = clip_q63_to_q31 (((q63_t) (Ibeta) * (cosVal)) >> 31);
  2942. /* Calculate pId by adding the two intermediate products 1 and 2 */
  2943. *pId = __QADD(product1, product2);
  2944. /* Calculate pIq by subtracting the two intermediate products 3 from 4 */
  2945. *pIq = __QSUB(product4, product3);
  2946. #endif
  2947. }
  2948. /**
  2949. * @}
  2950. */ // end of park group
  2951. void csky_q7_to_float(
  2952. q7_t * pSrc,
  2953. float32_t * pDst,
  2954. uint32_t blockSize);
  2955. /**
  2956. * @ingroup groupController
  2957. */
  2958. /**
  2959. * @defgroup inv_park Vector Inverse Park transform
  2960. * Inverse Park transform converts the input flux and torque components to two-coordinate vector.
  2961. *
  2962. * The function operates on a single sample of data and each call to the function returns the processed output.
  2963. * The library provides separate functions for Q31 and floating-point data types.
  2964. * \par Algorithm
  2965. * \image html parkInvFormula.gif
  2966. * where <code>pIalpha</code> and <code>pIbeta</code> are the stator vector components,
  2967. * <code>Id</code> and <code>Iq</code> are rotor vector components and <code>cosVal</code> and <code>sinVal</code> are the
  2968. * cosine and sine values of theta (rotor flux position).
  2969. * \par Fixed-Point Behavior
  2970. * Care must be taken when using the Q31 version of the Park transform.
  2971. * In particular, the overflow and saturation behavior of the accumulator used must be considered.
  2972. * Refer to the function specific documentation below for usage guidelines.
  2973. */
  2974. /**
  2975. * @addtogroup inv_park
  2976. * @{
  2977. */
  2978. /**
  2979. * @brief Floating-point Inverse Park transform
  2980. * @param[in] Id input coordinate of rotor reference frame d
  2981. * @param[in] Iq input coordinate of rotor reference frame q
  2982. * @param[out] pIalpha points to output two-phase orthogonal vector axis alpha
  2983. * @param[out] pIbeta points to output two-phase orthogonal vector axis beta
  2984. * @param[in] sinVal sine value of rotation angle theta
  2985. * @param[in] cosVal cosine value of rotation angle theta
  2986. */
  2987. __STATIC_INLINE void csky_inv_park_f32(
  2988. float32_t Id,
  2989. float32_t Iq,
  2990. float32_t * pIalpha,
  2991. float32_t * pIbeta,
  2992. float32_t sinVal,
  2993. float32_t cosVal)
  2994. {
  2995. /* Calculate pIalpha using the equation, pIalpha = Id * cosVal - Iq * sinVal */
  2996. *pIalpha = Id * cosVal - Iq * sinVal;
  2997. /* Calculate pIbeta using the equation, pIbeta = Id * sinVal + Iq * cosVal */
  2998. *pIbeta = Id * sinVal + Iq * cosVal;
  2999. }
  3000. /**
  3001. * @}
  3002. */ // end of inv_park group
  3003. /**
  3004. * @addtogroup inv_park
  3005. * @{
  3006. */
  3007. /**
  3008. * @brief Inverse Park transform for Q31 version
  3009. * @param[in] Id input coordinate of rotor reference frame d
  3010. * @param[in] Iq input coordinate of rotor reference frame q
  3011. * @param[out] pIalpha points to output two-phase orthogonal vector axis alpha
  3012. * @param[out] pIbeta points to output two-phase orthogonal vector axis beta
  3013. * @param[in] sinVal sine value of rotation angle theta
  3014. * @param[in] cosVal cosine value of rotation angle theta
  3015. *
  3016. * <b>Scaling and Overflow Behavior:</b>
  3017. * \par
  3018. * The function is implemented using an internal 32-bit accumulator.
  3019. * The accumulator maintains 1.31 format by truncating lower 31 bits of the intermediate multiplication in 2.62 format.
  3020. * There is saturation on the addition, hence there is no risk of overflow.
  3021. */
  3022. __STATIC_INLINE void csky_inv_park_q31(
  3023. q31_t Id,
  3024. q31_t Iq,
  3025. q31_t * pIalpha,
  3026. q31_t * pIbeta,
  3027. q31_t sinVal,
  3028. q31_t cosVal)
  3029. {
  3030. #ifdef CSKY_SIMD
  3031. __ASM volatile(
  3032. "rmul.s32.h t0, %0, %3\n\t"
  3033. "rmul.s32.h t1, %1, %2\n\t"
  3034. "sub.s32.s t0, t0, t1\n\t"
  3035. "st.w t0, (%4, 0x0)\n\t"
  3036. "rmul.s32.h t0, %0, %2\n\t"
  3037. "rmul.s32.h t1, %1, %3\n\t"
  3038. "add.s32.s t0, t0, t1\n\t"
  3039. "st.w t0, (%5, 0x0)\n\t"
  3040. ::"r"(Id),"r"(Iq),"r"(sinVal),"r"(cosVal),"r"(pIalpha),"r"(pIbeta)
  3041. :"t0","t1", "memory");
  3042. #else
  3043. q31_t product1, product2; /* Temporary variables used to store intermediate results */
  3044. q31_t product3, product4; /* Temporary variables used to store intermediate results */
  3045. /* Intermediate product is calculated by (Id * cosVal) */
  3046. product1 = clip_q63_to_q31 (((q63_t) (Id) * (cosVal)) >> 31);
  3047. /* Intermediate product is calculated by (Iq * sinVal) */
  3048. product2 = clip_q63_to_q31 (((q63_t) (Iq) * (sinVal)) >> 31);
  3049. /* Intermediate product is calculated by (Id * sinVal) */
  3050. product3 = clip_q63_to_q31 (((q63_t) (Id) * (sinVal)) >> 31);
  3051. /* Intermediate product is calculated by (Iq * cosVal) */
  3052. product4 = clip_q63_to_q31 (((q63_t) (Iq) * (cosVal)) >> 31);
  3053. /* Calculate pIalpha by using the two intermediate products 1 and 2 */
  3054. *pIalpha = __QSUB(product1, product2);
  3055. /* Calculate pIbeta by using the two intermediate products 3 and 4 */
  3056. *pIbeta = __QADD(product4, product3);
  3057. #endif
  3058. }
  3059. /**
  3060. * @}
  3061. */ // end of inv_park group
  3062. void csky_q31_to_float(
  3063. q31_t * pSrc,
  3064. float32_t * pDst,
  3065. uint32_t blockSize);
  3066. /**
  3067. * @ingroup groupInterpolation
  3068. */
  3069. /**
  3070. * @defgroup LinearInterpolate Linear Interpolation
  3071. *
  3072. * Linear interpolation is a method of curve fitting using linear polynomials.
  3073. * Linear interpolation works by effectively drawing a straight line between two neighboring samples and returning the appropriate point along that line
  3074. *
  3075. * \par
  3076. * \image html LinearInterp.gif "Linear interpolation"
  3077. *
  3078. * \par
  3079. * A Linear Interpolate function calculates an output value(y), for the input(x)
  3080. * using linear interpolation of the input values x0, x1( nearest input values) and the output values y0 and y1(nearest output values)
  3081. *
  3082. * \par Algorithm:
  3083. * <pre>
  3084. * y = y0 + (x - x0) * ((y1 - y0)/(x1-x0))
  3085. * where x0, x1 are nearest values of input x
  3086. * y0, y1 are nearest values to output y
  3087. * </pre>
  3088. *
  3089. * \par
  3090. * This set of functions implements Linear interpolation process
  3091. * for Q7, Q15, Q31, and floating-point data types. The functions operate on a single
  3092. * sample of data and each call to the function returns a single processed value.
  3093. * <code>S</code> points to an instance of the Linear Interpolate function data structure.
  3094. * <code>x</code> is the input sample value. The functions returns the output value.
  3095. *
  3096. * \par
  3097. * if x is outside of the table boundary, Linear interpolation returns first value of the table
  3098. * if x is below input range and returns last value of table if x is above range.
  3099. */
  3100. /**
  3101. * @addtogroup LinearInterpolate
  3102. * @{
  3103. */
  3104. /**
  3105. * @brief Process function for the floating-point Linear Interpolation Function.
  3106. * @param[in,out] S is an instance of the floating-point Linear Interpolation structure
  3107. * @param[in] x input sample to process
  3108. * @return y processed output sample.
  3109. *
  3110. */
  3111. __STATIC_INLINE float32_t csky_linear_interp_f32(
  3112. csky_linear_interp_instance_f32 * S,
  3113. float32_t x)
  3114. {
  3115. float32_t y;
  3116. float32_t x0, x1; /* Nearest input values */
  3117. float32_t y0, y1; /* Nearest output values */
  3118. float32_t xSpacing = S->xSpacing; /* spacing between input values */
  3119. int32_t i; /* Index variable */
  3120. float32_t *pYData = S->pYData; /* pointer to output table */
  3121. /* Calculation of index */
  3122. i = (int32_t) ((x - S->x1) / xSpacing);
  3123. if(i < 0)
  3124. {
  3125. /* Iniatilize output for below specified range as least output value of table */
  3126. y = pYData[0];
  3127. }
  3128. else if((uint32_t)i >= S->nValues)
  3129. {
  3130. /* Iniatilize output for above specified range as last output value of table */
  3131. y = pYData[S->nValues - 1];
  3132. }
  3133. else
  3134. {
  3135. /* Calculation of nearest input values */
  3136. x0 = S->x1 + i * xSpacing;
  3137. x1 = S->x1 + (i + 1) * xSpacing;
  3138. /* Read of nearest output values */
  3139. y0 = pYData[i];
  3140. y1 = pYData[i + 1];
  3141. /* Calculation of output */
  3142. y = y0 + (x - x0) * ((y1 - y0) / (x1 - x0));
  3143. }
  3144. /* returns output value */
  3145. return (y);
  3146. }
  3147. /**
  3148. * @}
  3149. */ // end of LinearInterpolate group
  3150. /**
  3151. * @addtogroup LinearInterpolate
  3152. * @{
  3153. */
  3154. /**
  3155. * @brief Process function for the Q31 Linear Interpolation Function.
  3156. * @param[in] pYData pointer to Q31 Linear Interpolation table
  3157. * @param[in] x input sample to process
  3158. * @param[in] nValues number of table values
  3159. * @return y processed output sample.
  3160. *
  3161. * \par
  3162. * Input sample <code>x</code> is in 12.20 format which contains 12 bits for table index and 20 bits for fractional part.
  3163. * This function can support maximum of table size 2^12.
  3164. *
  3165. */
  3166. __STATIC_INLINE q31_t csky_linear_interp_q31(
  3167. q31_t * pYData,
  3168. q31_t x,
  3169. uint32_t nValues)
  3170. {
  3171. q31_t y; /* output */
  3172. q31_t y0, y1; /* Nearest output values */
  3173. q31_t fract; /* fractional part */
  3174. int32_t index; /* Index to read nearest output values */
  3175. /* Input is in 12.20 format */
  3176. /* 12 bits for the table index */
  3177. /* Index value calculation */
  3178. index = ((x & (q31_t)0xFFF00000) >> 20);
  3179. if(index >= (int32_t)(nValues - 1))
  3180. {
  3181. return (pYData[nValues - 1]);
  3182. }
  3183. else if(index < 0)
  3184. {
  3185. return (pYData[0]);
  3186. }
  3187. else
  3188. {
  3189. /* 20 bits for the fractional part */
  3190. /* shift left by 11 to keep fract in 1.31 format */
  3191. fract = (x & 0x000FFFFF) << 11;
  3192. /* Read two nearest output values from the index in 1.31(q31) format */
  3193. y0 = pYData[index];
  3194. y1 = pYData[index + 1];
  3195. #ifdef CSKY_SIMD
  3196. /* Calculation of y0 * (1-fract) and y is in 2.30 format */
  3197. y = mult_32x32_keep32(y0, (0x7FFFFFFF - fract));
  3198. /* Calculation of y0 * (1-fract) + y1 *fract and y is in 2.30 format */
  3199. y = multAcc_32x32_keep32(y, y1, fract);
  3200. #else
  3201. /* Calculation of y0 * (1-fract) and y is in 2.30 format */
  3202. y = ((q31_t) ((q63_t) y0 * (0x7FFFFFFF - fract) >> 32));
  3203. /* Calculation of y0 * (1-fract) + y1 *fract and y is in 2.30 format */
  3204. y += ((q31_t) (((q63_t) y1 * fract) >> 32));
  3205. #endif
  3206. /* Convert y to 1.31 format */
  3207. return (y << 1u);
  3208. }
  3209. }
  3210. /**
  3211. * @}
  3212. */ // end of LinearInterpolate group
  3213. /**
  3214. * @addtogroup LinearInterpolate
  3215. * @{
  3216. */
  3217. /**
  3218. *
  3219. * @brief Process function for the Q15 Linear Interpolation Function.
  3220. * @param[in] pYData pointer to Q15 Linear Interpolation table
  3221. * @param[in] x input sample to process
  3222. * @param[in] nValues number of table values
  3223. * @return y processed output sample.
  3224. *
  3225. * \par
  3226. * Input sample <code>x</code> is in 12.20 format which contains 12 bits for table index and 20 bits for fractional part.
  3227. * This function can support maximum of table size 2^12.
  3228. *
  3229. */
  3230. __STATIC_INLINE q15_t csky_linear_interp_q15(
  3231. q15_t * pYData,
  3232. q31_t x,
  3233. uint32_t nValues)
  3234. {
  3235. q63_t y; /* output */
  3236. q15_t y0, y1; /* Nearest output values */
  3237. q31_t fract; /* fractional part */
  3238. int32_t index; /* Index to read nearest output values */
  3239. /* Input is in 12.20 format */
  3240. /* 12 bits for the table index */
  3241. /* Index value calculation */
  3242. index = ((x & (int32_t)0xFFF00000) >> 20);
  3243. if(index >= (int32_t)(nValues - 1))
  3244. {
  3245. return (pYData[nValues - 1]);
  3246. }
  3247. else if(index < 0)
  3248. {
  3249. return (pYData[0]);
  3250. }
  3251. else
  3252. {
  3253. /* 20 bits for the fractional part */
  3254. /* fract is in 12.20 format */
  3255. fract = (x & 0x000FFFFF);
  3256. /* Read two nearest output values from the index */
  3257. y0 = pYData[index];
  3258. y1 = pYData[index + 1];
  3259. #ifdef CSKY_SIMD
  3260. /* Calculation of y0 * (1-fract) and y is in 13.35 format */
  3261. y = mult_32x32_keep64(y0, (0xFFFFF - fract));
  3262. /* Calculation of (y0 * (1-fract) + y1 * fract) and y is in 13.35 format */
  3263. y = multAcc_32x32_keep64(y, y1, (fract));
  3264. #else
  3265. /* Calculation of y0 * (1-fract) and y is in 13.35 format */
  3266. y = ((q63_t) y0 * (0xFFFFF - fract));
  3267. /* Calculation of (y0 * (1-fract) + y1 * fract) and y is in 13.35 format */
  3268. y += ((q63_t) y1 * (fract));
  3269. #endif
  3270. /* convert y to 1.15 format */
  3271. return (q15_t) (y >> 20);
  3272. }
  3273. }
  3274. /**
  3275. * @}
  3276. */ // end of LinearInterpolate group
  3277. /**
  3278. * @addtogroup LinearInterpolate
  3279. * @{
  3280. */
  3281. /**
  3282. *
  3283. * @brief Process function for the Q7 Linear Interpolation Function.
  3284. * @param[in] pYData pointer to Q7 Linear Interpolation table
  3285. * @param[in] x input sample to process
  3286. * @param[in] nValues number of table values
  3287. * @return y processed output sample.
  3288. *
  3289. * \par
  3290. * Input sample <code>x</code> is in 12.20 format which contains 12 bits for table index and 20 bits for fractional part.
  3291. * This function can support maximum of table size 2^12.
  3292. */
  3293. __STATIC_INLINE q7_t csky_linear_interp_q7(
  3294. q7_t * pYData,
  3295. q31_t x,
  3296. uint32_t nValues)
  3297. {
  3298. q31_t y; /* output */
  3299. q7_t y0, y1; /* Nearest output values */
  3300. q31_t fract; /* fractional part */
  3301. uint32_t index; /* Index to read nearest output values */
  3302. /* Input is in 12.20 format */
  3303. /* 12 bits for the table index */
  3304. /* Index value calculation */
  3305. if (x < 0)
  3306. {
  3307. return (pYData[0]);
  3308. }
  3309. index = (x >> 20) & 0xfff;
  3310. if(index >= (nValues - 1))
  3311. {
  3312. return (pYData[nValues - 1]);
  3313. }
  3314. else
  3315. {
  3316. /* 20 bits for the fractional part */
  3317. /* fract is in 12.20 format */
  3318. fract = (x & 0x000FFFFF);
  3319. /* Read two nearest output values from the index and are in 1.7(q7) format */
  3320. y0 = pYData[index];
  3321. y1 = pYData[index + 1];
  3322. /* Calculation of y0 * (1-fract ) and y is in 13.27(q27) format */
  3323. y = ((y0 * (0xFFFFF - fract)));
  3324. /* Calculation of y1 * fract + y0 * (1-fract) and y is in 13.27(q27) format */
  3325. y += (y1 * fract);
  3326. /* convert y to 1.7(q7) format */
  3327. return (q7_t) (y >> 20);
  3328. }
  3329. }
  3330. /**
  3331. * @}
  3332. */ // end of LinearInterpolate group
  3333. float32_t csky_sin_f32(
  3334. float32_t x);
  3335. q31_t csky_sin_q31(
  3336. q31_t x);
  3337. q15_t csky_sin_q15(
  3338. q15_t x);
  3339. float32_t csky_cos_f32(
  3340. float32_t x);
  3341. q31_t csky_cos_q31(
  3342. q31_t x);
  3343. q15_t csky_cos_q15(
  3344. q15_t x);
  3345. csky_status csky_sqrt_f32(
  3346. float32_t in,
  3347. float32_t * pOut);
  3348. csky_status csky_sqrt_q31(
  3349. q31_t in,
  3350. q31_t * pOut);
  3351. csky_status csky_sqrt_q15(
  3352. q15_t in,
  3353. q15_t * pOut);
  3354. /*double format*/
  3355. typedef union _myNumber
  3356. {
  3357. q31_t i[2];
  3358. float64_t x;
  3359. }mynumber;
  3360. /* the coefficient for log2 table looh up*/
  3361. typedef union
  3362. {
  3363. q31_t i[5800];
  3364. float64_t x[2900];
  3365. }log2_cof1;
  3366. typedef union
  3367. {
  3368. q31_t i[4350];
  3369. float64_t x[2175];
  3370. }log2_cof2;
  3371. /* the coefficient for exp table looh up*/
  3372. typedef union
  3373. {
  3374. q31_t i[1424];
  3375. float64_t x[712];
  3376. }exp_cof1;
  3377. typedef union
  3378. {
  3379. q31_t i[2048];
  3380. float64_t x[1024];
  3381. }exp_cof2;
  3382. union ieee754_double
  3383. {
  3384. float64_t d;
  3385. struct
  3386. {
  3387. unsigned int mantissa1:32;
  3388. unsigned int mantissa0:20;
  3389. unsigned int exponent:11;
  3390. unsigned int negative:1;
  3391. } ieee;
  3392. struct
  3393. {
  3394. unsigned int mantissa1:32;
  3395. unsigned int mantissa0:19;
  3396. unsigned int quiet_nan:1;
  3397. unsigned int exponent:11;
  3398. unsigned int negative:1;
  3399. } ieee_nan;
  3400. };
  3401. typedef struct
  3402. {
  3403. q31_t e;
  3404. long d[40];
  3405. }mp_no;
  3406. float64_t csky_pow_f64(
  3407. float64_t x,
  3408. float64_t y);
  3409. float64_t csky_log_f64(
  3410. float64_t x);
  3411. float64_t csky_exp_f64(
  3412. float64_t x);
  3413. float64_t csky_pow2_f64(
  3414. float64_t x);
  3415. float64_t csky_log2_f64(
  3416. float64_t x);
  3417. float64_t csky_log10_f64(
  3418. float64_t x);
  3419. void csky_power_q31(
  3420. q31_t * pSrc,
  3421. uint32_t blockSize,
  3422. q63_t * pResult);
  3423. void csky_power_int32(
  3424. int32_t * pSrc,
  3425. uint32_t blockSize,
  3426. q63_t * pResult);
  3427. void csky_power_int32(
  3428. int32_t * pSrc,
  3429. uint32_t blockSize,
  3430. q63_t * pResult);
  3431. void csky_power_f32(
  3432. float32_t * pSrc,
  3433. uint32_t blockSize,
  3434. float32_t * pResult);
  3435. void csky_power_q15(
  3436. q15_t * pSrc,
  3437. uint32_t blockSize,
  3438. q63_t * pResult);
  3439. void csky_power_q7(
  3440. q7_t * pSrc,
  3441. uint32_t blockSize,
  3442. q31_t * pResult);
  3443. void csky_mean_q7(
  3444. q7_t * pSrc,
  3445. uint32_t blockSize,
  3446. q7_t * pResult);
  3447. void csky_mean_q15(
  3448. q15_t * pSrc,
  3449. uint32_t blockSize,
  3450. q15_t * pResult);
  3451. void csky_mean_q31(
  3452. q31_t * pSrc,
  3453. uint32_t blockSize,
  3454. q31_t * pResult);
  3455. void csky_mean_f32(
  3456. float32_t * pSrc,
  3457. uint32_t blockSize,
  3458. float32_t * pResult);
  3459. void csky_var_f32(
  3460. float32_t * pSrc,
  3461. uint32_t blockSize,
  3462. float32_t * pResult);
  3463. void csky_var_q31(
  3464. q31_t * pSrc,
  3465. uint32_t blockSize,
  3466. q31_t * pResult);
  3467. void csky_var_q15(
  3468. q15_t * pSrc,
  3469. uint32_t blockSize,
  3470. q15_t * pResult);
  3471. void csky_rms_f32(
  3472. float32_t * pSrc,
  3473. uint32_t blockSize,
  3474. float32_t * pResult);
  3475. void csky_rms_q31(
  3476. q31_t * pSrc,
  3477. uint32_t blockSize,
  3478. q31_t * pResult);
  3479. void csky_rms_q15(
  3480. q15_t * pSrc,
  3481. uint32_t blockSize,
  3482. q15_t * pResult);
  3483. void csky_std_f32(
  3484. float32_t * pSrc,
  3485. uint32_t blockSize,
  3486. float32_t * pResult);
  3487. void csky_std_q31(
  3488. q31_t * pSrc,
  3489. uint32_t blockSize,
  3490. q31_t * pResult);
  3491. void csky_std_q15(
  3492. q15_t * pSrc,
  3493. uint32_t blockSize,
  3494. q15_t * pResult);
  3495. void csky_cmplx_mag_f32(
  3496. float32_t * pSrc,
  3497. float32_t * pDst,
  3498. uint32_t numSamples);
  3499. void csky_cmplx_mag_q31(
  3500. q31_t * pSrc,
  3501. q31_t * pDst,
  3502. uint32_t numSamples);
  3503. void csky_cmplx_mag_q15(
  3504. q15_t * pSrc,
  3505. q15_t * pDst,
  3506. uint32_t numSamples);
  3507. void csky_cmplx_dot_prod_q15(
  3508. q15_t * pSrcA,
  3509. q15_t * pSrcB,
  3510. uint32_t numSamples,
  3511. q31_t * realResult,
  3512. q31_t * imagResult);
  3513. void csky_cmplx_dot_prod_q31(
  3514. q31_t * pSrcA,
  3515. q31_t * pSrcB,
  3516. uint32_t numSamples,
  3517. q63_t * realResult,
  3518. q63_t * imagResult);
  3519. void csky_cmplx_dot_prod_f32(
  3520. float32_t * pSrcA,
  3521. float32_t * pSrcB,
  3522. uint32_t numSamples,
  3523. float32_t * realResult,
  3524. float32_t * imagResult);
  3525. void csky_cmplx_mult_real_q15(
  3526. q15_t * pSrcCmplx,
  3527. q15_t * pSrcReal,
  3528. q15_t * pCmplxDst,
  3529. uint32_t numSamples);
  3530. void csky_cmplx_mult_real_q31(
  3531. q31_t * pSrcCmplx,
  3532. q31_t * pSrcReal,
  3533. q31_t * pCmplxDst,
  3534. uint32_t numSamples);
  3535. void csky_cmplx_mult_real_f32(
  3536. float32_t * pSrcCmplx,
  3537. float32_t * pSrcReal,
  3538. float32_t * pCmplxDst,
  3539. uint32_t numSamples);
  3540. void csky_min_q7(
  3541. q7_t * pSrc,
  3542. uint32_t blockSize,
  3543. q7_t * result,
  3544. uint32_t * index);
  3545. void csky_min_q15(
  3546. q15_t * pSrc,
  3547. uint32_t blockSize,
  3548. q15_t * pResult,
  3549. uint32_t * pIndex);
  3550. void csky_min_q31(
  3551. q31_t * pSrc,
  3552. uint32_t blockSize,
  3553. q31_t * pResult,
  3554. uint32_t * pIndex);
  3555. void csky_min_f32(
  3556. float32_t * pSrc,
  3557. uint32_t blockSize,
  3558. float32_t * pResult,
  3559. uint32_t * pIndex);
  3560. void csky_max_q7(
  3561. q7_t * pSrc,
  3562. uint32_t blockSize,
  3563. q7_t * pResult,
  3564. uint32_t * pIndex);
  3565. void csky_max_q15(
  3566. q15_t * pSrc,
  3567. uint32_t blockSize,
  3568. q15_t * pResult,
  3569. uint32_t * pIndex);
  3570. void csky_max_q31(
  3571. q31_t * pSrc,
  3572. uint32_t blockSize,
  3573. q31_t * pResult,
  3574. uint32_t * pIndex);
  3575. void csky_max_f32(
  3576. float32_t * pSrc,
  3577. uint32_t blockSize,
  3578. float32_t * pResult,
  3579. uint32_t * pIndex);
  3580. void csky_cmplx_mult_cmplx_q15(
  3581. q15_t * pSrcA,
  3582. q15_t * pSrcB,
  3583. q15_t * pDst,
  3584. uint32_t numSamples);
  3585. void csky_cmplx_mult_cmplx_q31(
  3586. q31_t * pSrcA,
  3587. q31_t * pSrcB,
  3588. q31_t * pDst,
  3589. uint32_t numSamples);
  3590. void csky_cmplx_mult_cmplx_f32(
  3591. float32_t * pSrcA,
  3592. float32_t * pSrcB,
  3593. float32_t * pDst,
  3594. uint32_t numSamples);
  3595. void csky_cmplx_mult_cmplx_re_q15(
  3596. q15_t * pSrcA,
  3597. q15_t * pSrcB,
  3598. q15_t * pDst,
  3599. uint32_t numSamples);
  3600. void csky_cmplx_mult_cmplx_re_q31(
  3601. q31_t * pSrcA,
  3602. q31_t * pSrcB,
  3603. q31_t * pDst,
  3604. uint32_t numSamples);
  3605. void csky_cmplx_mult_cmplx_re_f32(
  3606. float32_t * pSrcA,
  3607. float32_t * pSrcB,
  3608. float32_t * pDst,
  3609. uint32_t numSamples);
  3610. void csky_float_to_q31(
  3611. float32_t * pSrc,
  3612. q31_t * pDst,
  3613. uint32_t blockSize);
  3614. void csky_float_to_q15(
  3615. float32_t * pSrc,
  3616. q15_t * pDst,
  3617. uint32_t blockSize);
  3618. void csky_float_to_q7(
  3619. float32_t * pSrc,
  3620. q7_t * pDst,
  3621. uint32_t blockSize);
  3622. void csky_q31_to_q15(
  3623. q31_t * pSrc,
  3624. q15_t * pDst,
  3625. uint32_t blockSize);
  3626. void csky_q31_to_q7(
  3627. q31_t * pSrc,
  3628. q7_t * pDst,
  3629. uint32_t blockSize);
  3630. void csky_q15_to_float(
  3631. q15_t * pSrc,
  3632. float32_t * pDst,
  3633. uint32_t blockSize);
  3634. void csky_q15_to_q31(
  3635. q15_t * pSrc,
  3636. q31_t * pDst,
  3637. uint32_t blockSize);
  3638. void csky_q15_to_q7(
  3639. q15_t * pSrc,
  3640. q7_t * pDst,
  3641. uint32_t blockSize);
  3642. /**
  3643. * @ingroup groupInterpolation
  3644. */
  3645. /**
  3646. * @defgroup BilinearInterpolate Bilinear Interpolation
  3647. *
  3648. * Bilinear interpolation is an extension of linear interpolation applied to a two dimensional grid.
  3649. * The underlying function <code>f(x, y)</code> is sampled on a regular grid and the interpolation process
  3650. * determines values between the grid points.
  3651. * Bilinear interpolation is equivalent to two step linear interpolation, first in the x-dimension and then in the y-dimension.
  3652. * Bilinear interpolation is often used in image processing to rescale images.
  3653. * The CSI DSP library provides bilinear interpolation functions for Q7, Q15, Q31, and floating-point data types.
  3654. *
  3655. * <b>Algorithm</b>
  3656. * \par
  3657. * The instance structure used by the bilinear interpolation functions describes a two dimensional data table.
  3658. * For floating-point, the instance structure is defined as:
  3659. * <pre>
  3660. * typedef struct
  3661. * {
  3662. * uint16_t numRows;
  3663. * uint16_t numCols;
  3664. * float32_t *pData;
  3665. * } csky_bilinear_interp_instance_f32;
  3666. * </pre>
  3667. *
  3668. * \par
  3669. * where <code>numRows</code> specifies the number of rows in the table;
  3670. * <code>numCols</code> specifies the number of columns in the table;
  3671. * and <code>pData</code> points to an array of size <code>numRows*numCols</code> values.
  3672. * The data table <code>pTable</code> is organized in row order and the supplied data values fall on integer indexes.
  3673. * That is, table element (x,y) is located at <code>pTable[x + y*numCols]</code> where x and y are integers.
  3674. *
  3675. * \par
  3676. * Let <code>(x, y)</code> specify the desired interpolation point. Then define:
  3677. * <pre>
  3678. * XF = floor(x)
  3679. * YF = floor(y)
  3680. * </pre>
  3681. * \par
  3682. * The interpolated output point is computed as:
  3683. * <pre>
  3684. * f(x, y) = f(XF, YF) * (1-(x-XF)) * (1-(y-YF))
  3685. * + f(XF+1, YF) * (x-XF)*(1-(y-YF))
  3686. * + f(XF, YF+1) * (1-(x-XF))*(y-YF)
  3687. * + f(XF+1, YF+1) * (x-XF)*(y-YF)
  3688. * </pre>
  3689. * Note that the coordinates (x, y) contain integer and fractional components.
  3690. * The integer components specify which portion of the table to use while the
  3691. * fractional components control the interpolation processor.
  3692. *
  3693. * \par
  3694. * if (x,y) are outside of the table boundary, Bilinear interpolation returns zero output.
  3695. */
  3696. /**
  3697. * @addtogroup BilinearInterpolate
  3698. * @{
  3699. */
  3700. /**
  3701. *
  3702. * @brief Floating-point bilinear interpolation.
  3703. * @param[in,out] S points to an instance of the interpolation structure.
  3704. * @param[in] X interpolation coordinate.
  3705. * @param[in] Y interpolation coordinate.
  3706. * @return out interpolated value.
  3707. */
  3708. __STATIC_INLINE float32_t csky_bilinear_interp_f32(
  3709. const csky_bilinear_interp_instance_f32 * S,
  3710. float32_t X,
  3711. float32_t Y)
  3712. {
  3713. float32_t out;
  3714. float32_t f00, f01, f10, f11;
  3715. float32_t *pData = S->pData;
  3716. int32_t xIndex, yIndex, index;
  3717. float32_t xdiff, ydiff;
  3718. float32_t b1, b2, b3, b4;
  3719. xIndex = (int32_t) X;
  3720. yIndex = (int32_t) Y;
  3721. /* Care taken for table outside boundary */
  3722. /* Returns zero output when values are outside table boundary */
  3723. if(xIndex < 0 || xIndex > (S->numRows - 1) || yIndex < 0 || yIndex > (S->numCols - 1))
  3724. {
  3725. return (0);
  3726. }
  3727. /* Calculation of index for two nearest points in X-direction */
  3728. index = (xIndex - 1) + (yIndex - 1) * S->numCols;
  3729. /* Read two nearest points in X-direction */
  3730. f00 = pData[index];
  3731. f01 = pData[index + 1];
  3732. /* Calculation of index for two nearest points in Y-direction */
  3733. index = (xIndex - 1) + (yIndex) * S->numCols;
  3734. /* Read two nearest points in Y-direction */
  3735. f10 = pData[index];
  3736. f11 = pData[index + 1];
  3737. /* Calculation of intermediate values */
  3738. b1 = f00;
  3739. b2 = f01 - f00;
  3740. b3 = f10 - f00;
  3741. b4 = f00 - f01 - f10 + f11;
  3742. /* Calculation of fractional part in X */
  3743. xdiff = X - xIndex;
  3744. /* Calculation of fractional part in Y */
  3745. ydiff = Y - yIndex;
  3746. /* Calculation of bi-linear interpolated output */
  3747. out = b1 + b2 * xdiff + b3 * ydiff + b4 * xdiff * ydiff;
  3748. /* return to application */
  3749. return (out);
  3750. }
  3751. /**
  3752. * @}
  3753. */ // end of BilinearInterpolate group
  3754. /**
  3755. * @addtogroup BilinearInterpolate
  3756. * @{
  3757. */
  3758. /**
  3759. *
  3760. * @brief Q31 bilinear interpolation.
  3761. * @param[in,out] S points to an instance of the interpolation structure.
  3762. * @param[in] X interpolation coordinate in 12.20 format.
  3763. * @param[in] Y interpolation coordinate in 12.20 format.
  3764. * @return out interpolated value.
  3765. */
  3766. __STATIC_INLINE q31_t csky_bilinear_interp_q31(
  3767. csky_bilinear_interp_instance_q31 * S,
  3768. q31_t X,
  3769. q31_t Y)
  3770. {
  3771. q31_t out; /* Temporary output */
  3772. q31_t acc = 0; /* output */
  3773. q31_t xfract, yfract; /* X, Y fractional parts */
  3774. q31_t x1, x2, y1, y2; /* Nearest output values */
  3775. int32_t rI, cI; /* Row and column indices */
  3776. q31_t *pYData = S->pData; /* pointer to output table values */
  3777. uint32_t nCols = S->numCols; /* num of rows */
  3778. /* Input is in 12.20 format */
  3779. /* 12 bits for the table index */
  3780. /* Index value calculation */
  3781. rI = ((X & (q31_t)0xFFF00000) >> 20);
  3782. /* Input is in 12.20 format */
  3783. /* 12 bits for the table index */
  3784. /* Index value calculation */
  3785. cI = ((Y & (q31_t)0xFFF00000) >> 20);
  3786. /* Care taken for table outside boundary */
  3787. /* Returns zero output when values are outside table boundary */
  3788. if(rI < 0 || rI > (S->numRows - 1) || cI < 0 || cI > (S->numCols - 1))
  3789. {
  3790. return (0);
  3791. }
  3792. /* 20 bits for the fractional part */
  3793. /* shift left xfract by 11 to keep 1.31 format */
  3794. xfract = (X & 0x000FFFFF) << 11u;
  3795. /* Read two nearest output values from the index */
  3796. x1 = pYData[(rI) + (int32_t)nCols * (cI) ];
  3797. x2 = pYData[(rI) + (int32_t)nCols * (cI) + 1];
  3798. /* 20 bits for the fractional part */
  3799. /* shift left yfract by 11 to keep 1.31 format */
  3800. yfract = (Y & 0x000FFFFF) << 11u;
  3801. /* Read two nearest output values from the index */
  3802. y1 = pYData[(rI) + (int32_t)nCols * (cI + 1) ];
  3803. y2 = pYData[(rI) + (int32_t)nCols * (cI + 1) + 1];
  3804. #ifdef CSKY_SIMD
  3805. /* Calculation of x1 * (1-xfract ) * (1-yfract) and acc is in 3.29(q29) format */
  3806. out = mult_32x32_keep32(x1, (0x7FFFFFFF - xfract));
  3807. acc = mult_32x32_keep32(out, (0x7FFFFFFF - yfract));
  3808. /* x2 * (xfract) * (1-yfract) in 3.29(q29) and adding to acc */
  3809. out = mult_32x32_keep32(x2, (0x7FFFFFFF - yfract));
  3810. acc = multAcc_32x32_keep32(acc, out, xfract);
  3811. /* y1 * (1 - xfract) * (yfract) in 3.29(q29) and adding to acc */
  3812. out = mult_32x32_keep32(y1, (0x7FFFFFFF - xfract));
  3813. acc = multAcc_32x32_keep32(acc, out, yfract);
  3814. /* y2 * (xfract) * (yfract) in 3.29(q29) and adding to acc */
  3815. out = mult_32x32_keep32(y2, xfract);
  3816. acc = multAcc_32x32_keep32(acc, out, yfract);
  3817. #else
  3818. /* Calculation of x1 * (1-xfract ) * (1-yfract) and acc is in 3.29(q29) format */
  3819. out = ((q31_t) (((q63_t) x1 * (0x7FFFFFFF - xfract)) >> 32));
  3820. acc = ((q31_t) (((q63_t) out * (0x7FFFFFFF - yfract)) >> 32));
  3821. /* x2 * (xfract) * (1-yfract) in 3.29(q29) and adding to acc */
  3822. out = ((q31_t) ((q63_t) x2 * (0x7FFFFFFF - yfract) >> 32));
  3823. acc += ((q31_t) ((q63_t) out * (xfract) >> 32));
  3824. /* y1 * (1 - xfract) * (yfract) in 3.29(q29) and adding to acc */
  3825. out = ((q31_t) ((q63_t) y1 * (0x7FFFFFFF - xfract) >> 32));
  3826. acc += ((q31_t) ((q63_t) out * (yfract) >> 32));
  3827. /* y2 * (xfract) * (yfract) in 3.29(q29) and adding to acc */
  3828. out = ((q31_t) ((q63_t) y2 * (xfract) >> 32));
  3829. acc += ((q31_t) ((q63_t) out * (yfract) >> 32));
  3830. #endif
  3831. /* Convert acc to 1.31(q31) format */
  3832. return ((q31_t)(acc << 2));
  3833. }
  3834. /**
  3835. * @}
  3836. */ // end of BilinearInterpolate group
  3837. /**
  3838. * @addtogroup BilinearInterpolate
  3839. * @{
  3840. */
  3841. /**
  3842. * @brief Q15 bilinear interpolation.
  3843. * @param[in,out] S points to an instance of the interpolation structure.
  3844. * @param[in] X interpolation coordinate in 12.20 format.
  3845. * @param[in] Y interpolation coordinate in 12.20 format.
  3846. * @return out interpolated value.
  3847. */
  3848. __STATIC_INLINE q15_t csky_bilinear_interp_q15(
  3849. csky_bilinear_interp_instance_q15 * S,
  3850. q31_t X,
  3851. q31_t Y)
  3852. {
  3853. q63_t acc = 0; /* output */
  3854. q31_t out; /* Temporary output */
  3855. q15_t x1, x2, y1, y2; /* Nearest output values */
  3856. q31_t xfract, yfract; /* X, Y fractional parts */
  3857. int32_t rI, cI; /* Row and column indices */
  3858. q15_t *pYData = S->pData; /* pointer to output table values */
  3859. uint32_t nCols = S->numCols; /* num of rows */
  3860. /* Input is in 12.20 format */
  3861. /* 12 bits for the table index */
  3862. /* Index value calculation */
  3863. rI = ((X & (q31_t)0xFFF00000) >> 20);
  3864. /* Input is in 12.20 format */
  3865. /* 12 bits for the table index */
  3866. /* Index value calculation */
  3867. cI = ((Y & (q31_t)0xFFF00000) >> 20);
  3868. /* Care taken for table outside boundary */
  3869. /* Returns zero output when values are outside table boundary */
  3870. if(rI < 0 || rI > (S->numRows - 1) || cI < 0 || cI > (S->numCols - 1))
  3871. {
  3872. return (0);
  3873. }
  3874. /* 20 bits for the fractional part */
  3875. /* xfract should be in 12.20 format */
  3876. xfract = (X & 0x000FFFFF);
  3877. /* Read two nearest output values from the index */
  3878. x1 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI) ];
  3879. x2 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI) + 1];
  3880. /* 20 bits for the fractional part */
  3881. /* yfract should be in 12.20 format */
  3882. yfract = (Y & 0x000FFFFF);
  3883. /* Read two nearest output values from the index */
  3884. y1 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI + 1) ];
  3885. y2 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI + 1) + 1];
  3886. /* Calculation of x1 * (1-xfract ) * (1-yfract) and acc is in 13.51 format */
  3887. /* x1 is in 1.15(q15), xfract in 12.20 format and out is in 13.35 format */
  3888. /* convert 13.35 to 13.31 by right shifting and out is in 1.31 */
  3889. #ifdef CSKY_SIMD
  3890. out = mult_32x32_dext_4(x1, (0xFFFFF - xfract));
  3891. acc = mult_32x32_keep64(out, (0xFFFFF - yfract));
  3892. /* x2 * (xfract) * (1-yfract) in 1.51 and adding to acc */
  3893. out = mult_32x32_dext_4(x2, (0xFFFFF - yfract));
  3894. acc = multAcc_32x32_keep64(acc, out, (xfract));
  3895. /* y1 * (1 - xfract) * (yfract) in 1.51 and adding to acc */
  3896. out = mult_32x32_dext_4(y1, (0xFFFFF - xfract));
  3897. acc = multAcc_32x32_keep64(acc, out, (yfract));
  3898. /* y2 * (xfract) * (yfract) in 1.51 and adding to acc */
  3899. out = mult_32x32_dext_4(y2, (xfract));
  3900. acc = multAcc_32x32_keep64(acc, out, (yfract));
  3901. #else
  3902. out = (q31_t) (((q63_t) x1 * (0xFFFFF - xfract)) >> 4u);
  3903. acc = ((q63_t) out * (0xFFFFF - yfract));
  3904. /* x2 * (xfract) * (1-yfract) in 1.51 and adding to acc */
  3905. out = (q31_t) (((q63_t) x2 * (0xFFFFF - yfract)) >> 4u);
  3906. acc += ((q63_t) out * (xfract));
  3907. /* y1 * (1 - xfract) * (yfract) in 1.51 and adding to acc */
  3908. out = (q31_t) (((q63_t) y1 * (0xFFFFF - xfract)) >> 4u);
  3909. acc += ((q63_t) out * (yfract));
  3910. /* y2 * (xfract) * (yfract) in 1.51 and adding to acc */
  3911. out = (q31_t) (((q63_t) y2 * (xfract)) >> 4u);
  3912. acc += ((q63_t) out * (yfract));
  3913. #endif
  3914. /* acc is in 13.51 format and down shift acc by 36 times */
  3915. /* Convert out to 1.15 format */
  3916. return ((q15_t)(acc >> 36));
  3917. }
  3918. /**
  3919. * @}
  3920. */ // end of BilinearInterpolate group
  3921. void test(q7_t *pSrc, q7_t *pDst);
  3922. /**
  3923. * @addtogroup BilinearInterpolate
  3924. * @{
  3925. */
  3926. /**
  3927. * @brief Q7 bilinear interpolation.
  3928. * @param[in,out] S points to an instance of the interpolation structure.
  3929. * @param[in] X interpolation coordinate in 12.20 format.
  3930. * @param[in] Y interpolation coordinate in 12.20 format.
  3931. * @return out interpolated value.
  3932. */
  3933. __STATIC_INLINE q7_t csky_bilinear_interp_q7(
  3934. csky_bilinear_interp_instance_q7 * S,
  3935. q31_t X,
  3936. q31_t Y)
  3937. {
  3938. q63_t acc = 0; /* output */
  3939. q31_t out; /* Temporary output */
  3940. q31_t xfract, yfract; /* X, Y fractional parts */
  3941. q7_t x1, x2, y1, y2; /* Nearest output values */
  3942. int32_t rI, cI; /* Row and column indices */
  3943. q7_t *pYData = S->pData; /* pointer to output table values */
  3944. uint32_t nCols = S->numCols; /* num of rows */
  3945. /* Input is in 12.20 format */
  3946. /* 12 bits for the table index */
  3947. /* Index value calculation */
  3948. rI = ((X & (q31_t)0xFFF00000) >> 20);
  3949. /* Input is in 12.20 format */
  3950. /* 12 bits for the table index */
  3951. /* Index value calculation */
  3952. cI = ((Y & (q31_t)0xFFF00000) >> 20);
  3953. /* Care taken for table outside boundary */
  3954. /* Returns zero output when values are outside table boundary */
  3955. if(rI < 0 || rI > (S->numRows - 1) || cI < 0 || cI > (S->numCols - 1))
  3956. {
  3957. return (0);
  3958. }
  3959. /* 20 bits for the fractional part */
  3960. /* xfract should be in 12.20 format */
  3961. xfract = (X & (q31_t)0x000FFFFF);
  3962. /* Read two nearest output values from the index */
  3963. x1 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI) ];
  3964. x2 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI) + 1];
  3965. /* 20 bits for the fractional part */
  3966. /* yfract should be in 12.20 format */
  3967. yfract = (Y & (q31_t)0x000FFFFF);
  3968. /* Read two nearest output values from the index */
  3969. y1 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI + 1) ];
  3970. y2 = pYData[((uint32_t)rI) + nCols * ((uint32_t)cI + 1) + 1];
  3971. /* Calculation of x1 * (1-xfract ) * (1-yfract) and acc is in 16.47 format */
  3972. out = ((x1 * (0xFFFFF - xfract)));
  3973. #ifdef CSKY_SIMD
  3974. acc = multAcc_32x32_keep64(acc, out, (0xFFFFF - yfract));
  3975. /* x2 * (xfract) * (1-yfract) in 2.22 and adding to acc */
  3976. out = ((x2 * (0xFFFFF - yfract)));
  3977. acc = multAcc_32x32_keep64(acc, out, xfract);
  3978. /* y1 * (1 - xfract) * (yfract) in 2.22 and adding to acc */
  3979. out = ((y1 * (0xFFFFF - xfract)));
  3980. acc = multAcc_32x32_keep64(acc, out, yfract);
  3981. /* y2 * (xfract) * (yfract) in 2.22 and adding to acc */
  3982. out = ((y2 * (yfract)));
  3983. acc = multAcc_32x32_keep64(acc, out, xfract);
  3984. #else
  3985. acc = (((q63_t) out * (0xFFFFF - yfract)));
  3986. /* x2 * (xfract) * (1-yfract) in 2.22 and adding to acc */
  3987. out = ((x2 * (0xFFFFF - yfract)));
  3988. acc += (((q63_t) out * (xfract)));
  3989. /* y1 * (1 - xfract) * (yfract) in 2.22 and adding to acc */
  3990. out = ((y1 * (0xFFFFF - xfract)));
  3991. acc += (((q63_t) out * (yfract)));
  3992. /* y2 * (xfract) * (yfract) in 2.22 and adding to acc */
  3993. out = ((y2 * (yfract)));
  3994. acc += (((q63_t) out * (xfract)));
  3995. #endif
  3996. /* acc in 16.47 format and down shift by 40 to convert to 1.7 format */
  3997. return ((q7_t)(acc >> 40));
  3998. }
  3999. /**
  4000. * @}
  4001. */ // end of BilinearInterpolate group
  4002. /**
  4003. * @ingroup groupMath
  4004. */
  4005. /**
  4006. * @defgroup ShiftRight Right Shift
  4007. *
  4008. * Shift the input value to right with appointed bits, its basic format is:
  4009. * <pre>
  4010. * a = (a) >> (shift), 1 =< shift <= bitof(a) - 1.
  4011. * </pre>
  4012. * The basic format is only designed for q31.
  4013. *
  4014. * and the extended format should be rounding to +inf:
  4015. * <pre>
  4016. * a = (a + (1<<(shift - 1)) >> (shift), 1 =< shift <= bitof(a) - 1.
  4017. * </pre>
  4018. *
  4019. * which are designed for q31, q31 positive and q63.
  4020. */
  4021. /**
  4022. * @addtogroup ShiftRight
  4023. * @{
  4024. */
  4025. /**
  4026. * @brief right shift Q31 version
  4027. * @param[in] a input value to be shift.
  4028. * @param[in] shift input positive value, the number of bits to be shift.
  4029. * @param[out] result the shifted a.
  4030. *
  4031. * <b>Scaling and Overflow Behavior:</b>
  4032. * \par
  4033. * The function is only used for right shift. So, the value of shift is
  4034. * between[1,31].
  4035. */
  4036. __STATIC_INLINE q31_t csky_shr_q31(
  4037. q31_t a,
  4038. q31_t shift)
  4039. {
  4040. q31_t res;
  4041. #ifdef CSKY_SIMD
  4042. __ASM volatile(
  4043. "asr %0, %1, %2\n\t"
  4044. :"=r"(res), "=r"(a),"=r"(shift):"0"(res), "1"(a), "2"(shift));
  4045. #else
  4046. res = ((a) >> (shift));
  4047. #endif
  4048. return res;
  4049. }
  4050. #define SHR(a, shift) csky_shr_q31(a, shift)
  4051. /**
  4052. * @}
  4053. */ // end of ShiftRight group
  4054. /**
  4055. * @addtogroup ShiftRight
  4056. * @{
  4057. */
  4058. /**
  4059. * @brief right shift Q31 version
  4060. * @param[in] a input value to be shift.
  4061. * @param[in] shift input positive value, the number of bits to be shift.
  4062. * @param[out] result the shifted a.
  4063. *
  4064. * <b>Scaling and Overflow Behavior:</b>
  4065. * \par
  4066. * The function is only used for right shift. So, the value of shift is
  4067. * between[1,31]. And the output value is rounding to +inf.
  4068. */
  4069. __STATIC_INLINE q31_t csky_pshr_q31(
  4070. q31_t a,
  4071. q31_t shift)
  4072. {
  4073. q31_t res;
  4074. #ifdef CSKY_SIMD
  4075. __ASM volatile(
  4076. "asr.s32.r %0, %1, %2\n\t"
  4077. :"=r"(res), "=r"(a),"=r"(shift):"0"(res), "1"(a), "2"(shift));
  4078. #else
  4079. res = (a >= 0?(SHR((a) + (1<<(shift - 1)), shift))\
  4080. :(SHR((a) + ((1<<shift)>>1) -1, shift)));
  4081. #endif
  4082. return res;
  4083. }
  4084. /**
  4085. * @}
  4086. */ // end of ShiftRight group
  4087. /**
  4088. * @addtogroup ShiftRight
  4089. * @{
  4090. */
  4091. /**
  4092. * @brief right shift Q31 version
  4093. * @param[in] a input positive value to be shift.
  4094. * @param[in] shift input positive value, the number of bits to be shift.
  4095. * @param[out] result the shifted a.
  4096. *
  4097. * <b>Scaling and Overflow Behavior:</b>
  4098. * \par
  4099. * The function is only used for right shift. So, the value of shift is
  4100. * between[1,31]. And the output value is rounding to +inf.
  4101. */
  4102. __STATIC_INLINE q31_t csky_pshr_pos_q31(
  4103. q31_t a,
  4104. q31_t shift)
  4105. {
  4106. q31_t res;
  4107. #ifdef CSKY_SIMD
  4108. __ASM volatile(
  4109. "asr.s32.r %0, %1, %2\n\t"
  4110. :"=r"(res), "=r"(a),"=r"(shift):"0"(res), "1"(a), "2"(shift));
  4111. #else
  4112. res = SHR((a) + (1<<(shift - 1)), shift);
  4113. #endif
  4114. return res;
  4115. }
  4116. /**
  4117. * @}
  4118. */ // end of ShiftRight group
  4119. /**
  4120. * @addtogroup ShiftRight
  4121. * @{
  4122. */
  4123. /**
  4124. * @brief right shift Q63 version
  4125. * @param[in] a input value to be shift.
  4126. * @param[in] shift input positive value, the number of bits to be shift.
  4127. * @param[out] result the shifted a.
  4128. *
  4129. * <b>Scaling and Overflow Behavior:</b>
  4130. * \par
  4131. * The function is only used for right shift. So, the value of shift is
  4132. * between[1,63]. And the output value is rounding to +inf.
  4133. */
  4134. __STATIC_INLINE q63_t csky_pshr_q63(
  4135. q63_t a,
  4136. q31_t shift)
  4137. {
  4138. q63_t res;
  4139. #ifdef CSKY_SIMD
  4140. __ASM volatile(
  4141. "subi t0, %2, 1\n\t"
  4142. "cmphsi t0, 32\n\t"
  4143. "bt 1f\n\t"
  4144. "movi t1, 1\n\t"
  4145. "lsl t0, t1, t0\n\t"
  4146. "movi t1, 0\n\t"
  4147. "add.s64.s %1, %1, t0\n\t"
  4148. "dext %0, %1, %R1, %2\n\t"
  4149. "asr %R0, %R1, %2\n\t"
  4150. "br 2f\n\t"
  4151. "1:\n\t"
  4152. "subi %2, %2, 32\n\t"
  4153. "subi t0, t0, 32\n\t"
  4154. "movi t1, 1\n\t"
  4155. "lsl t1, t1, t0\n\t"
  4156. "add.s32.s %R1, %R1, t1\n\t"
  4157. "asr %0, %R1, %2\n\t"
  4158. "asri %R0, %R1, 31\n\t"
  4159. "2:\n\t"
  4160. :"=r"(res), "=r"(a),"=r"(shift):"0"(res), "1"(a), "2"(shift):"t0", "t1");
  4161. #else
  4162. res = (a >= 0?(SHR((a) + ((q63_t)1<<(shift - 1)), shift))\
  4163. :(SHR((a) + (((q63_t)1<<shift)>>1) -1, shift)));
  4164. #endif
  4165. return res;
  4166. }
  4167. /**
  4168. * @}
  4169. */ // end of ShiftRight group
  4170. //#define SHR(a, shift) csky_shr_q31(a, shift)
  4171. #define PSHR(a, shift) csky_pshr_q31(a, shift)
  4172. #define PSHR_POSITIVE(a, shift) csky_pshr_pos_q31(a, shift)
  4173. #define PSHR64(a, shift) csky_pshr_q63(a, shift)
  4174. #ifdef CSKY_SIMD
  4175. #else
  4176. /* SMMLAR */
  4177. #define multAcc_32x32_keep32_R(a, x, y) \
  4178. a = (q31_t) (((((q63_t) a) << 32) + ((q63_t) x * y) + 0x80000000LL ) >> 32)
  4179. /* SMMLSR */
  4180. #define multSub_32x32_keep32_R(a, x, y) \
  4181. a = (q31_t) (((((q63_t) a) << 32) - ((q63_t) x * y) + 0x80000000LL ) >> 32)
  4182. /* SMMULR */
  4183. #define mult_32x32_keep32_R(a, x, y) \
  4184. a = (q31_t) (((q63_t) x * y + 0x80000000LL ) >> 32)
  4185. /* SMMLA */
  4186. #define multAcc_32x32_keep32(a, x, y) \
  4187. a += (q31_t) (((q63_t) x * y) >> 32)
  4188. /* SMMLS */
  4189. #define multSub_32x32_keep32(a, x, y) \
  4190. a -= (q31_t) (((q63_t) x * y) >> 32)
  4191. /* SMMUL */
  4192. #define mult_32x32_keep32(a, x, y) \
  4193. a = (q31_t) (((q63_t) x * y ) >> 32)
  4194. #endif
  4195. #ifdef __cplusplus
  4196. }
  4197. #endif
  4198. #endif /* _CSKY_MATH_H */
  4199. /**
  4200. *
  4201. * End of file.
  4202. */