12 Replies Latest reply on Nov 7, 2016 12:42 AM by user_1377889

    Average calculation using DFB - PSoC5




      I'm looking for the solution of the following problem using Digital Filter Block in PSoC5:


      Input: Data block (72bit = 3 x 24bit) consist of 3 channels (24bit)


      Function: 3 channels data processing using DFB
                       - Data blocks coming from the PSoC as stream with the 10kHz frequency 
                       - For N data blocks the average value of each channel should be calculated 


      Output: After N data blocks processing in DFB, average values for all 3 channels should be generated (3 x 32bit).


      In the attachment problem is graphically described. I would be very thankful if someone could help me with the assembler code or useful examples.


      Best regards,



        • 1. Re: Average calculation using DFB - PSoC5

          Digital filter just has got 2 channels. 10kHz could be done on a PSoC5 with a program, the CPU is fast enough.





          • 2. Re: Average calculation using DFB - PSoC5



            (i) What is the source of 24-bit data? Is it a digital  bus? 


            (ii) It appears that  all operators are linear, therefore sum of 3 channels can be taken before averaging of each channel, and having the same result without spending PSoC resources.

            • 3. Re: Average calculation using DFB - PSoC5

              odissey1, thank you for your interest for my problem.


              1. Data comes from the ADC (3 sensors - channels) via SPI to DMA.
              2. The Internal Psoc Main Processor we cannot use because it has to do other things and has no resources for that.


              For the processing in the DFB we have the following possibilities:
              1. DFB input is 72-bit stream (data block with all of 3 channels samples)
              2. DFB input is 24-bit stream (data block is just one channel sample)


              Ad. 1
              We write 72-bit in one stream and processing it in DFB. After n-iteration, average values (results) for particular channel are stored in RAM.


              Ad. 2:
              We write 24 bit in DMA (just one channel) and trigger the DFB. When the DFB is finished with the first processing of first channel
              we proceed with the next 24 bit (next channel) and then the same for the third. After (3 x n)-iteration, results are stored in the RAM.


              Very important is that the entire procedure (all 72 bits) takes not longer than 100µs. But I mean that this is not a problem, because the DFB itself needs just some ns for processing.


              Thank you very much for your help in advance!

              • 4. Re: Average calculation using DFB - PSoC5



                now I understand the problem better. In short, you want to improve 24-bit ADC resolution by oversampling.


                The reason I asked is that I recently made a component for ADC_SAR oversampling and thought that it could be useful here (which is not the case). The component takes data from digital bus (not DMA), uses IIR filter (you want FIR filter) and realized in PLD (no space for 3 32-bit blocks).


                I see some issues using DFB for your goal. The DFB has 24-bit calculation engine - the output will be 24 bit no matter how long the average is. If the goal was to ultimately obtain ~32-bit resolution, it won't happen. Then there are only two channels in DFB, so you have to somehow buffer or interleave incoming data etc., which adds headacke.


                I believe that this task fits well into UDB Datapath, consuming 4 UDB cells for each ADC (50% of all UDB resources total). (DMA-FIFO-ALU-shift.) Unfortunately, I don't have example for that. ADC_SAR in PSoC4 has such averaging feature, so you may inspect how it's made there.


                I align with Bob Marlowe on that 3x10kHz is not terribly fast, and ISR+CPU will be able to handle simple arithmetic involved. I would try this first. As an example, take a look on this BLDC control app. note, which uses PSOC4 ISR for controlling 4 phases at 10kHz rate.



                • 5. Re: Average calculation using DFB - PSoC5



                  Yes we want to improve the resolution by reducing the signal noise. 
                  The ADCs are extern and well filtered using hardware for the high frequencies (~10kHz 3dB).
                  We want to achieve a damping for 1kHz therefore we have to use a digital filter. HW filter changes are not possible because of additional complex reasons. 
                  The PSOC5 itself is used extensively. All the UDBs are used and the processor itself stands under heavy load. 
                  At the moment the damping is working and it is realized with DMA and the processor itself but we need processor's power for other open issues.  
                  As you see the DFB is a tempting option. What I should mention is that we need only 24-bit as output of the DFB because the shift to a 32-bit value can be done by DMA itself and this is already working.
                  Also important is that we can shift the lower 8-bit of a sample value out by DMA (only noise) before sending it to the DFB. In combination with the fact that we have only a maximum of 100 samples for the mean value we ensure that an overflow cannot occur.      


                  As I see this now we have two possibilities:
                  First is to implement two counters in the DFB one for the channels and one for the samples of each channel. We need three DFB RAM fields for storage the three subtotals. Everything is synchronized so we know each value of the corresponding channel due to the channel counter. If the second sample counter reach his maximum than we have to divide the subtotal by the counter value and send the result out. Due to the synchronisation the DMA, after the DFB, knows to which channel this result value correspond and can save the result in the global RAM at the right position. 


                  The second possibility is that we save each input value (3 blocks one for each channel) in the DFB RAM and sort the values. If the sample counter has his maximum we can choose than the medium value. In thise case no sum is needed but we need much more space in the RAM. Therefore a n of about 10 is the maximum which would be achievable. This solution is therefore not the preferred one.


                  What we achieved until now is to multiply and divide an input value in DFB and send the result to the output. What we will try to do next is to store an input value in the DFB RAM and read it out and write to the output.


                  Hope this detailed description was helpful. It would be quite happy if you can help us to achieve the goal and I have an open mind for better solutions of this problem.

                  • 6. Re: Average calculation using DFB - PSoC5

                    I ´ve prepared three useful simple examples for  DFB programming in assembler. Examples might be useful for anyone who is a beginner in DFB assembler. Very recommended is also the article on the web site: http://arttools.blogspot.co.at/2016/02/.


                    With these three examples you can find the answers on the following questions:


                    1. How to multiply input value with pre-defined constant stored at DFB RAM?


                    2. How to read data from RAM and transfer it to the DFB output register?


                    3. How to save data present at the input Stage A register into the specific RAM location?

                    • 7. Re: Average calculation using DFB - PSoC5

                      Thank you for assembler codes, a demo project would be great!

                      • 8. Re: Average calculation using DFB - PSoC5

                        A few comments:


                        When waiting for values to arrive at the input register the loopjump  jmpl(in1,Multiply) should be used, otherwise the code will fall through to next block even with no new data present in the input.


                        eob is only necessary to specify for unconditional jumps.



                        • 9. Re: Average calculation using DFB - PSoC5

                          Try the attached DFB assembler code, it reads from staging register A, adds the value into one of three RAM locations (channels) and switching to the next channel.


                          When 8 samples have been summed, the totals are output to holding register A, and the accumulation is restarted.


                          Best regards



                          • 10. Re: Average calculation using DFB - PSoC5

                            Your approach reduces the output frequency by factor of eight (8).


                            When you program a gliding average the output frequency will be the same as the input frequency.





                            • 11. Re: Average calculation using DFB - PSoC5

                              It does reduce the output frequency by 8, but this is what the OP asked for.

                              • 12. Re: Average calculation using DFB - PSoC5

                                Magnus, I have no experiences in programming the DFB. But I thought that a gliding average filter would be quite useful as a filter implementation.