|
| 1 | +#include <stress.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <math.h> |
| 4 | +#include <stdbool.h> // For using the bool type |
| 5 | +#include <float.h> |
| 6 | +#include <stdint.h> |
| 7 | + |
| 8 | + |
| 9 | +#define N 3 // Define the size of the input signal |
| 10 | + |
| 11 | +// Function to compute the DCT-II |
| 12 | +void dct(float input[N], float output[N]) { |
| 13 | + int k, n; |
| 14 | + float alpha; |
| 15 | + |
| 16 | + for (k = 0; k < N; k++) { |
| 17 | + output[k] = 0.0; |
| 18 | + |
| 19 | + for (n = 0; n < N; n++) { |
| 20 | + output[k] += input[n] * cosf(M_PI / N * (n + 0.5) * k); |
| 21 | + } |
| 22 | + |
| 23 | + // Scaling factor for normalization |
| 24 | + alpha = (k == 0) ? sqrtf(1.0 / N) : sqrtf(2.0 / N); |
| 25 | + output[k] *= alpha; |
| 26 | + |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Function to compute the Inverse DCT (IDCT) |
| 31 | +void idct(float input[N], float output[N]) { |
| 32 | + int k, n; |
| 33 | + float alpha; |
| 34 | + |
| 35 | + for (n = 0; n < N; n++) { |
| 36 | + output[n] = 0.0; |
| 37 | + |
| 38 | + for (k = 0; k < N; k++) { |
| 39 | + alpha = (k == 0) ? sqrtf(1.0 / N) : sqrtf(2.0 / N); |
| 40 | + output[n] += alpha * input[k] * cosf(M_PI / N * (n + 0.5) * k); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Function to check if the IDCT output matches the original input within a tolerance |
| 46 | +bool check_reconstruction(float input[N], float output[N], float tolerance) { |
| 47 | + for (int i = 0; i < N; i++) { |
| 48 | + if (fabs(input[i] - output[i]) > tolerance) { |
| 49 | + return false; // Failure if the difference exceeds tolerance |
| 50 | + } |
| 51 | + } |
| 52 | + return true; // Success if all values are within tolerance |
| 53 | +} |
| 54 | + |
| 55 | +// Main function to test DCT and IDCT implementation |
| 56 | +int main() { |
| 57 | + stress_test_start(); |
| 58 | + // float input[N] = {255, 231, 189, 165, 122, 100, 85, 52}; // Example input signal |
| 59 | + float input[N] = {255, 231, 189}; // Example input signal |
| 60 | + float dct_output[N], idct_output[N]; |
| 61 | + float tolerance = 1e-4; // Define an acceptable error tolerance |
| 62 | + bool failure; |
| 63 | + |
| 64 | + // Perform the DCT |
| 65 | + dct(input, dct_output); |
| 66 | + |
| 67 | + // Perform the Inverse DCT |
| 68 | + idct(dct_output, idct_output); |
| 69 | + |
| 70 | + // Check if the IDCT output matches the original input |
| 71 | + failure = !check_reconstruction(input, idct_output, tolerance); |
| 72 | + |
| 73 | + // Check if reconstruction was successful |
| 74 | + if (failure) { |
| 75 | + return 1; // Exit with failure code |
| 76 | + } |
| 77 | + stress_test_end(); |
| 78 | + return 0; // Exit with success code |
| 79 | +} |
| 80 | + |
| 81 | +to run this linker script needs to be changed to |
| 82 | + |
| 83 | + // /* INCLUDE ../../generated/output_format.ld */ |
| 84 | + |
| 85 | + // OUTPUT_FORMAT("elf32-littleriscv") |
| 86 | + |
| 87 | + // ENTRY(_start) |
| 88 | + |
| 89 | + // __DYNAMIC = 0; |
| 90 | + |
| 91 | + // /* INCLUDE ../../generated/regions.ld */ |
| 92 | + |
| 93 | + // MEMORY { |
| 94 | + // vexriscv_debug : ORIGIN = 0xf00f0000, LENGTH = 0x00000100 |
| 95 | + // dff : ORIGIN = 0x00000000, LENGTH = 0x00000400 |
| 96 | + // dff2_part1 : ORIGIN = 0x00000400, LENGTH = 0x00000100 |
| 97 | + // dff2_part2 : ORIGIN = 0x00000500, LENGTH = 0x00000100 |
| 98 | + // flash : ORIGIN = 0x10000000, LENGTH = 0x01000000 |
| 99 | + // mprj : ORIGIN = 0x30000000, LENGTH = 0x00100000 |
| 100 | + // hk : ORIGIN = 0x26000000, LENGTH = 0x00100000 |
| 101 | + // csr : ORIGIN = 0xf0000000, LENGTH = 0x00010000 |
| 102 | + // } |
| 103 | + |
| 104 | + // SECTIONS |
| 105 | + // { |
| 106 | + // .text : { |
| 107 | + // _ftext = .; |
| 108 | + // *(.vectors) |
| 109 | + // *(.text*) |
| 110 | + // *(.rodata*) |
| 111 | + // *(.srodata.cst16) |
| 112 | + // *(.srodata.cst8) |
| 113 | + // *(.srodata.cst4) |
| 114 | + // *(.srodata.cst2) |
| 115 | + // *(.srodata .srodata.*) |
| 116 | + // . = ALIGN(8); |
| 117 | + // _etext = .; |
| 118 | + // } > flash |
| 119 | + |
| 120 | + |
| 121 | + // .data : |
| 122 | + // { |
| 123 | + // _fdata = .; |
| 124 | + // *(.sdata .sdata.* .gnu.linkonce.s.*) |
| 125 | + // *(.data*); |
| 126 | + // _edata = .; |
| 127 | + // PROVIDE( __global_pointer$ = . + 0x800 ); |
| 128 | + |
| 129 | + // } > dff,dff2_part1 AT > flash |
| 130 | + |
| 131 | + // .bss (NOLOAD) : |
| 132 | + // { |
| 133 | + // _fbss = .; |
| 134 | + // *(.bss*) |
| 135 | + // *(COMMON) |
| 136 | + // _ebss = .; |
| 137 | + // _end = .; |
| 138 | + // } > dff,dff2_part1 |
| 139 | + // } |
| 140 | + |
| 141 | + // PROVIDE(_fstack = ORIGIN(dff2_part2) + LENGTH(dff2_part2)); |
| 142 | + |
| 143 | + // PROVIDE(_fdata_rom = LOADADDR(.data)); |
| 144 | + // PROVIDE(_edata_rom = LOADADDR(.data) + SIZEOF(.data)); |
| 145 | + |
| 146 | +// and stub function also |
| 147 | + |
| 148 | + |
| 149 | + |
0 commit comments