]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ep-processor-libraries/dsplib.git/blob - ti/dsplib/src/DSP_autocor/c66/DSP_autocor_cn.c
DSPLIB: optimized signal processing functions for TI DSPs
[ep-processor-libraries/dsplib.git] / ti / dsplib / src / DSP_autocor / c66 / DSP_autocor_cn.c
1 /* ======================================================================= */
2 /*  NAME                                                                   */
3 /*      DSP_autocor_cn -- Autocorrelation                                  */
4 /*                                                                         */
5 /*   USAGE                                                                 */
6 /*        This routine has the following C prototype:                      */
7 /*                                                                         */
8 /*        void DSP_autocor_cn (                                            */
9 /*            short *restrict r,                                           */
10 /*            const short *restrict x,                                     */
11 /*            int          nx,                                             */
12 /*            int          nr                                              */
13 /*         );                                                              */
14 /*                                                                         */
15 /*        r[nr]   : Output array                                           */
16 /*        x[nr+nx]: Input array. The first nr elements are assumed to be 0 */
17 /*        nx      : Length of autocorrelation                              */
18 /*        nr      : Number of lags                                         */
19 /*                                                                         */
20 /*                                                                         */
21 /*     DESCRIPTION                                                         */
22 /*        This routine performs an autocorrelation of an input vector      */
23 /*        x. The length of the autocorrelation is nx samples. Since nr     */
24 /*        such autocorrelations are performed, input vector x needs to be  */
25 /*        of length nx + nr. This produces nr output results which are     */
26 /*        stored in an output array r.                                     */
27 /*                                                                         */
28 /*        The following diagram illustrates how the correlations are       */
29 /*        obtained.                                                        */
30 /*                                                                         */
31 /*        Example for nr=8, nx=24:                                         */
32 /*        0       nr                  nx+nr-1                              */
33 /*        |-------|----------------------|  <- x[]                         */
34 /*        |       |----------------------|  -> r[0]                        */
35 /*        |      |----------------------|   -> r[1]                        */
36 /*        |     |----------------------|    -> r[2]                        */
37 /*        |    |----------------------|     -> r[3]                        */
38 /*        |   |----------------------|      -> r[4]                        */
39 /*        |  |----------------------|       -> r[5]                        */
40 /*        | |----------------------|        -> r[6]                        */
41 /*                                                                         */
42 /*        Note that x[0] is never used, but is required for padding to     */
43 /*        make x[nr] double-word aligned.                                  */
44 /*                                                                         */
45 /*   ASSUMPTIONS                                                           */
46 /*        The first nr elements are assumed to be 0.                       */
47 /*                                                                         */
48 /* Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/  */ 
49 /*                                                                         */
50 /*                                                                         */
51 /*  Redistribution and use in source and binary forms, with or without     */
52 /*  modification, are permitted provided that the following conditions     */
53 /*  are met:                                                               */
54 /*                                                                         */
55 /*    Redistributions of source code must retain the above copyright       */
56 /*    notice, this list of conditions and the following disclaimer.        */
57 /*                                                                         */
58 /*    Redistributions in binary form must reproduce the above copyright    */
59 /*    notice, this list of conditions and the following disclaimer in the  */
60 /*    documentation and/or other materials provided with the               */
61 /*    distribution.                                                        */
62 /*                                                                         */
63 /*    Neither the name of Texas Instruments Incorporated nor the names of  */
64 /*    its contributors may be used to endorse or promote products derived  */
65 /*    from this software without specific prior written permission.        */
66 /*                                                                         */
67 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS    */
68 /*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT      */
69 /*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  */
70 /*  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT   */
71 /*  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  */
72 /*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT       */
73 /*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  */
74 /*  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  */
75 /*  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT    */
76 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  */
77 /*  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   */
78 /*                                                                         */
79 /* ======================================================================= */
81 #pragma CODE_SECTION(DSP_autocor_cn, ".text:ansi");
83 #include "DSP_autocor_cn.h"
85 void DSP_autocor_cn (
86     short *restrict r,
87     const short *restrict x,
88     int nx,
89     int nr
90 )
91 {
92    int i, k;
93    int sum;
95    #ifndef NOASSUME
96    _nassert (nr % 4 == 0);
97    #endif
99    /*----------------------------------------------------------------------*/
100    /* Outer loop that iterates for every output. There are 'nr' output     */
101    /* samples to be computed. Hence this loop iterates 'nr' times. 'nr'    */
102    /* needs to be a multiple of 4. This information is conveyed to the     */
103    /* compiler.                                                            */
104    /*----------------------------------------------------------------------*/ 
105    for (i = 0; i < nr; i++) {
106       sum = 0;
108       #ifndef NOASSUME
109       _nassert(nx % 8 == 0);
110       _nassert((int)(x) % 8 == 0);
111       _nassert((int)(r) % 4 == 0);
112       _nassert(nr % 4 == 0);
113       #pragma MUST_ITERATE(8,,4);
114       #endif
116       /*-------------------------------------------------------------------*/
117       /*  Compute one autocorrelation. Each autocorrelation has 'nx' terms */
118       /*  'nx' is assumed to be a multiple of 8.                           */
119       /*-------------------------------------------------------------------*/
120       for (k = nr; k < nx + nr; k++)
121           sum += x[k] * x[k-i];
123       /*-------------------------------------------------------------------*/
124       /* Shift out autocorrelation sum taking into account Q15 math.       */
125       /*-------------------------------------------------------------------*/
126       r[i] = sum  >> 15;
127    }
130 /* ======================================================================== */
131 /*  End of file:  DSP_autocor_cn.c                                          */
132 /* ------------------------------------------------------------------------ */
133 /*            Copyright (c) 2011 Texas Instruments, Incorporated.           */
134 /*                           All Rights Reserved.                           */
135 /* ======================================================================== */