summaryrefslogtreecommitdiff
path: root/objects/wip/pitch_detect_fft.axo
blob: 769262821ea15b49fd47095404b4416ea6189ee0 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<objdefs>
   <obj.normal id="pitch detect 128" uuid="89eda14ecd003f22288812e9843218f2d020b7ad" sha="e1ec4fbea98f37a5be48b1f7d3522ed1f7fe3229">
      <sDescription>spectral analyzer display using 128 input points fft</sDescription>
      <author>Johannes Taelman, Ricardo Wurmus</author>
      <license>BSD</license>
      <inlets>
         <frac32buffer name="in" description="input"/>
         <bool32 name="hold" description="hold"/>
      </inlets>
      <outlets>
        <int32 name="pitch" description="pitch"/>
      </outlets>
      <params/>
      <attribs/>
      <code.declaration><![CDATA[
#define N 128
int32_t inbuf[N];
int32_t outbuf[N];
// must be twice as wide because the result is
// stored like {real[0], imag[0], real[1], imag[1]...}
int32_t fftbuf[2*N];
arm_rfft_instance_q31 rfft;
arm_cfft_radix4_instance_q31 cfft;
int32_t state;
int32_t max_val = 0;
uint32_t max_a = 0;

/**    
 * @brief Maximum value of a Q31 vector.    
 * @param[in]       *pSrc points to the input vector    
 * @param[in]       blockSize length of the input vector    
 * @param[out]      *pResult maximum value returned here    
 * @param[out]      *pIndex index of maximum value returned here    
 * @return none.    
 */

void arm_max_q31(
  q31_t * pSrc,
  uint32_t blockSize,
  q31_t * pResult,
  uint32_t * pIndex)
{
#ifndef ARM_MATH_CM0_FAMILY

  /* Run the below code for Cortex-M4 and Cortex-M3 */
  q31_t maxVal1, maxVal2, out;                   /* Temporary variables to store the output value. */
  uint32_t blkCnt, outIndex, count;              /* loop counter */

  /* Initialise the count value. */
  count = 0u;
  /* Initialise the index value to zero. */
  outIndex = 0u;
  /* Load first input value that act as reference value for comparision */
  out = *pSrc++;

  /* Loop unrolling */
  blkCnt = (blockSize - 1u) >> 2u;

  /* Run the below code for Cortex-M4 and Cortex-M3 */
  while(blkCnt > 0u)
  {
    /* Initialize maxVal to the next consecutive values one by one */
    maxVal1 = *pSrc++;

    maxVal2 = *pSrc++;

    /* compare for the maximum value */
    if(out < maxVal1)
    {
      /* Update the maximum value and its index */
      out = maxVal1;
      outIndex = count + 1u;
    }

    maxVal1 = *pSrc++;

    /* compare for the maximum value */
    if(out < maxVal2)
    {
      /* Update the maximum value and its index */
      out = maxVal2;
      outIndex = count + 2u;
    }

    maxVal2 = *pSrc++;

    /* compare for the maximum value */
    if(out < maxVal1)
    {
      /* Update the maximum value and its index */
      out = maxVal1;
      outIndex = count + 3u;
    }

    /* compare for the maximum value */
    if(out < maxVal2)
    {
      /* Update the maximum value and its index */
      out = maxVal2;
      outIndex = count + 4u;
    }

    count += 4u;

    /* Decrement the loop counter */
    blkCnt--;
  }

  /* if (blockSize - 1u) is not multiple of 4 */
  blkCnt = (blockSize - 1u) % 4u;

#else

  /* Run the below code for Cortex-M0 */
  q31_t maxVal1, out;                            /* Temporary variables to store the output value. */
  uint32_t blkCnt, outIndex;                     /* loop counter */

  /* Initialise the index value to zero. */
  outIndex = 0u;
  /* Load first input value that act as reference value for comparision */
  out = *pSrc++;

  blkCnt = (blockSize - 1u);

#endif /* #ifndef ARM_MATH_CM0_FAMILY */

  while(blkCnt > 0u)
  {
    /* Initialize maxVal to the next consecutive values one by one */
    maxVal1 = *pSrc++;

    /* compare for the maximum value */
    if(out < maxVal1)
    {
      /* Update the maximum value and it's index */
      out = maxVal1;
      outIndex = blockSize - blkCnt;
    }

    /* Decrement the loop counter */
    blkCnt--;

  }

  /* Store the maximum value and its index into destination pointers */
  *pResult = out;
  *pIndex = outIndex;
}

]]></code.declaration>
      <code.init><![CDATA[
int i;
for(i=0;i<N;i++) inbuf[i]=0;
for(i=0;i<N;i++) outbuf[i]=0;
state = 0;
arm_rfft_init_q31(&rfft, &cfft, N,0,1);
]]></code.init>
      <code.krate><![CDATA[
if (state<N){
  // fill buffer
  int i;
  for(i=0;i<BUFSIZE;i++) {
    inbuf[state++] = inlet_in[i];
  }
} else {
  // compute FFT
  arm_rfft_q31(&rfft, (q31_t *)inbuf, (q31_t *)fftbuf);
  // compute magnitude
  arm_cmplx_mag_q31(fftbuf, outbuf, N);

  // second half is conjugates, so we only look at N/2
  arm_max_q31(outbuf, N/2, &max_val, &max_a);

  outlet_pitch = max_a;
  state = 0;
}
]]></code.krate>
   </obj.normal>
</objdefs>