co averaging basic sharc

8
Averaging Filter Basic SHARC assembly and adding automated tests Example of project development on SHARC using C++ and assembly Planned for Tutorial Tuesday 1 st October 1 Concept “Single Point Copy” Audio Filter Cooperative operating system used SHARC uTTCOS guarantees that only one task will run at a time Means, tasks ARE NOT swapped out but runinturn with the exception of ONE preemptive task (which interrupts other tasks) You can automatically generate files for a uTTCOS project in ENCM515/Assign1 directory using uTTCOS_Helper plugin Hidden hardware capture of audio signal causes an interrupt that places audio values in a known location You must add a preemptive task to process the audio signal to the uTTCOS todo list of tasks Preemptive tasks must have fast ‘inandout fast’ characteristics – absolute minimum time spend in preemptive task. Must be guaranteed to finish with 50% of 1 system TIC 2 Add Preemptive task to uTTCOS todo list. Use to test audio equipment in lab. Use of include path operation means only 1 header files across 3 projects 3 Project should compile with Copy_Left( ) stub The code will not run in the lab because of a logical defect) What’s the defect? RECAP ‐‐Learn optimization via basic DSP algorithm “Average last N points” (trivial LP filter) Requirements Output signal of filter y[n] is average of last N input values x[nj], 0 <= j < N y[n] = ( (x[n] + x[n1] ……..x[n – (N1)]) ) / N Need to store (and update) last N audio values captured Need to store in firstin firstout (FIFO) buffer Problem ‐‐ audio start up transient How do you handle the first “N” values? Assume that x[1], x[2] …. X[n – (N1)] = 0. Basically means you prefill the FIFO buffer with zeros y[0] = ( x[0] + 0 + 0 + 0 ) / N; 4

Upload: others

Post on 23-Dec-2021

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Co Averaging Basic SHARC

Averaging FilterBasic SHARC assembly

and adding automated testsExample of project development  on 

SHARC using C++ and assembly

Planned for Tutorial Tuesday 1st October

1

Concept “Single Point Copy” Audio Filter

• Co‐operative operating system used– SHARC uTTCOS guarantees that only one task will run at a time – Means, tasks ARE NOT swapped out but run‐in‐turn with the 

exception of ONE pre‐emptive task (which interrupts other tasks) 

• You can automatically generate files for a uTTCOS project in ENCM515/Assign1 directory using uTTCOS_Helper plug‐in– Hidden hardware capture of audio signal causes an interrupt 

that places audio values in a known location• You must add a pre‐emptive task to process the audio 

signal to the uTTCOS to‐do list of tasks– Pre‐emptive tasks must have fast  ‘in‐and‐out fast’ 

characteristics – absolute minimum time spend in pre‐emptive task. Must be guaranteed to finish with 50% of 1 system TIC

2

Add Pre‐emptive task to uTTCOS to‐do list. Use to test audio equipment in lab.

Use of include path operation means only 1 header files across 3 projects

3

Project shouldcompile withCopy_Left( ) stub

• The code will not run in the lab because of a logical defect)

• What’s the defect?

RECAP ‐‐Learn optimization via basic DSP algorithm“Average last N points” (trivial LP filter)

Requirements– Output signal of filter y[n]  is 

average of last N input values  x[n‐j],  0 <= j < N – y[n] = ( (x[n] + x[n‐1] ……..x[n – (N‐1)]) ) / N

Need to store (and update) last N audio values captured– Need to store in first‐in first‐out (FIFO) buffer

Problem  ‐‐ audio start up transient– How do you handle the first “N” values?

• Assume that  x[‐1], x[‐2] …. X[n – (N‐1)] = 0.• Basically means you pre‐fill the FIFO buffer with zeros• y[0] = ( x[0] + 0 + 0 + 0 ) / N;

4

Page 2: Co Averaging Basic SHARC

RECAP ‐‐ Code Design

• Discard oldest value in FIFO (oldest was first in, so first out

• This provides room to add new value to FIFO buffer• Output calculated by averaging all values in FIFO 

• Note N will be set as small number (e.g. N = 16) during some parts of test, and set large when timing done.– Don’t hand in labs handed in with poor timing test (D)

• Use no magic numbers use in code– No loops involving  for (j = 0; j < 1024; j++)– Use (j = 0; j < N; j++) where N is declared in Assign1.h so a single N is used across project including C, C++ and ASM

5

Develop a personal software processto minimize mistakes and wasted time• Do a code review for syntax errors

– Make a list of errors so that you can identify YOUR most common mistakes AND STOP making them 

– One less repeated error per laboratory minimum• The number of syntax errors the compiler finds after your code review is related to the number of logical (unfound) defects in your code.

• The number of syntax errors the compiler finds after your code review is related to the amount of time you will waste debugging your code looking for those hidden defects you did not find.

• Watts Humphreys PSP – If spend 30 minutes coding then spend 25% of 30 minutes on review

6

RECAP ‐‐ Understanding C properly is a key to real‐time programming

• What does this code mean?  ‐‐ Keyword ‘static’

• It means– When loading the program and data into the processor’s data and program memory using the embedded system BOOT sequence, make permanent private space ‘in general memory’ for an array FIFO[ ].  

– As part of the embedded system BOOT sequence set all the values to zero before starting the program

• NOTE: This setting to zeroes is NOT a run‐time operation• NOTE: If restart the program (rather than reloading it), then ZEROS are not stored in FIFO[ ] – unknown E.B. values are stored there. 

7

Now we can try out the code• Build Assign1Library project , then CLEAN and build Assign1 

project to force it to use new library (Eclipse bug)

• Averaging removes high frequencies.

• Old vinyl records (1920) lacked high frequency components

• Assignment 1 : – What value of N to make 2011 music sound like 1920?– What does this sort of averaging do, and why?

8

Page 3: Co Averaging Basic SHARC

Build Library – Clean and Build project‐‐ Expect a linker error to be reported as no new 

code is present• Check that error message is what you expect

9

Missing function is NAME‐MANGLED meaning thanLinker is looking for C++ code and not assembly code.Typical means that “header file is wrong”ASK ME WHAT IS WRONG WITH HEADER FILEAND THEN NOTE IN IN YOUR NOTES

Understanding the code we used

• This processor has two kinds of memory– dm – data memory– pm – program memory

• HARVARD architecture of processor– Ability to access data memory and program memory at the same time. 

– Major speed improvement over von Neumann architecture

10

Accesses can be made directly to a specific memory address

• Just because you can access memory this way, does not mean that this is the fastest code

#define _LeftChannel_In1    0x40???? // define is automatically done by the linker 

11

Assembler message with adjustOutput volume is zero – WHY???

12

Page 4: Co Averaging Basic SHARC

Output volume is zero after we do the multiply

• Need to switch to a testing environment to work out why.– Actually, we can make a guess. 

• We are doing ‘integer mathematics’ (a.k.a. Blackfin)• Blackfin and MIPS 

– 300 cycles for FADD via special floating point subroutines• SHARC is ‘floating point ‘ processor

– 1 cycle for FADD via special floating point instructions 

• In future, we will set up tests first so that we know what the answer will be from complex algorithms. 13

F2 = F2 * F5 (float multiplication)R2 = R2 * R5 (two types of integer)

Avoid this refactoring defectNote CCES warns you (some what)

14

Assembler is ‘lying’ (Disassembly window)Only 1 in 5 is ‘real float’ instruction

• Memory stores bit patterns – not floats or integers• Constants are bit patterns ‐‐ not integers or floats 15

NO NEED TO USESILICON FORUNNECESARY INSTRUCTIONS

XP‐Inspired Life cycle (see Smith et al articles on 515 web‐site)

• Design – wish‐list of ‘stories’ to implement• Write Tests to show how code should work• Write C code to satisfy tests• Generate resource chart based on processor architecture to calculate best ‘theoretical’ speed– If ‘theoretical speed’ meets your ‘real‐time requirements’ you, then worth while trying to optimize!. 

– If ‘theoretical speed’ does not work for you then ‘find a different algorithm’, optimization is not going to help.

• Use already written tests to prove that (highly optimized) assembly code WORKS as well as being fast

16

Page 5: Co Averaging Basic SHARC

Lets try the approach with CopySingle( )• Assign1_Tests  – convert to E‐Unit using 

Open‐CCES‐ame Plug‐in• First test will be for ‘CopySingleCPP’• Change include path for .h to include Assign1_Library and add 

(debug version) Assign1_Library.dlb

17

Inside ‘Assign1_Test’ projectThere are .h and .dlb links into Library

Make LINKS to files in Assign1 Librarydirectory NOT copies of files  and libraries

18

Modify tests to validate C++ and assembly copy routines

• Confusing – Cntrl‐SPACE completion knew about LeftCPP( ) etc – but look at all error warnings 19

Did a project clean and then build

Do code review to find final error

This was arefactoring

errorwhen I changed file 

namesChange CONTROL and NOTIFY macros 

to include CPP(See next slide)

20

Page 6: Co Averaging Basic SHARC

Big errors on Build and Link

• We are no longer running with a real audio interface (All variables defined in uTTCOS_Library used in Assign1 project)

• Actually good – If we don’t know the actual audio values coming in, then we can’t test to see if we are processing them correctly

• Solution – Build a mock audio device and add the code to the project. 21

Here is the Mock Audio DeviceAsk me how do I fix the error?

• Why is my coding mistake an “error” and not a “defect”?

• Did I do a code review before compiling?

22

Run the tests ‐‐Missing Open‐CCES‐ame features Check if ran to completion – Click on ErrorRight click on blue bar to show line numbers

23CLICK ON ERROR TO GO TO  FAILING TEST

24

Straight forward to addmore tests to see if there is a pattern in the defect result

Why is it a code defect and not a code error?

Page 7: Co Averaging Basic SHARC

Is there a pattern in the error messages?If exists, then hints at defect to solve

• Output value is wrong, but the wrongness scales with the input value size

25

Use Open‐CESS‐ame to add new test group for Averaging code – 3 styles of tests

26

Time test – measure in us Must be less than 20 us per point (1 audio channel)

27

Un‐automated,

But we need tocollect details anddon`t have to do much analysis in lab

Interesting CCES code ran much slower than VDSP code

28

CCES has differentC++ device buffercharacteristicsapparently forprintf( )

Page 8: Co Averaging Basic SHARC

Let`s try optimizing the C++ code• Right click Assign1Library.cpp and make a copy as Assign1LibraryOptimized.cpp• Change function names e.g. AverageSingleAudioValue_LeftCPP to 

AverageSingleAudioValue_LeftCPPOptimized – delete other functions in file• Add new function to include file• Right click on Assign1LibraryOptimized.cpp and select properties• Under C/C++ Build | Settings     select General and then Enable Optimization

– Note – we are just optimizing the C++ code in this file and not in the whole library

29

Add new test for optimized codeModify EUNIT main to show only failures

Don’t understand why there is an indicated error.

Tried  CLEAN

30

Big difference  between optimized and unoptimized C++Question is the code  still working now its optimized (Actually never tested before)

31

New test – used tocompare to ‘theoretical’Analysis of cycles used in code

Remember – also timing the time around the loop and the time to jump in and out of routines

Testing the code’s correctness should have been done before optimization

• Warning – if FIFO_SIZE is small –then will pass timing tests

32