Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Coding homework please help i will rate thumbs up Given the data structure and functions in the ring.h file and the tests in test_ring.c,

C Coding homework please help i will rate thumbs up

Given the data structure and functions in the ring.h file and the tests in test_ring.c, implement a ring buffer in a file named ring.c that passes the tests in test_ring and provide the time complexities in Big-Oh for each function.

You will also find this information helpful for the ring_apply_fir() function:

image text in transcribed-----

GIVEN CODE:

ring.h:

#ifndef _RING_H_

#define _RING_H_

/* Data structure containing a ring buffer */

struct ring {

unsigned int size; // Capacity of the buffer

unsigned int read; // Next index to be read from

unsigned int write; // Next index to be written to

short *data; // Array containing the buffer values

};

/**

* Create a new ring buffer

* Size parameter is the capacity of the buffer

* Returns a pointer to the created struct, or NULL on failure

*/

struct ring * ring_create(int size);

/**

* Returns the number of items currently in the ring buffer

* If there is an error, return 0

*/

int ring_size(struct ring *ring);

/**

* Enqueues an item onto the ring buffer

* In the case of an error (invalid parameters or a full buffer), return -1

* Return 0 on success

*/

int ring_enqueue(struct ring *ring, short data);

/**

* Dequeues an item from the ring buffer

* Removes an item from the buffer and stores it at the location pointed to by datap

* In the case of an error (invalid parametres or an empty buffer), return -1

* Returns 0 on success

*/

int ring_dequeue(struct ring *ring, short *datap);

/**

* Computes the weighted sum of all items in the buffer

* You may assume that the length of the weights array is the same as the buffer capacity

* If the buffer is not full, do not compute the sum and instead return 0

* Return 0 on an error (invalid parameters) or the weighted sum otherwise

*/

double ring_apply_fir(struct ring *ring, double *weights);

/**

* Frees memory associated with the passed ring buffer

*/

void ring_free(struct ring *ring);

#endif /* _RING_H_ */

---------------

test_ring.c:

#include

#include

#include "ring.h"

#define SIZE 3

#define VAL 42

#define ASSERT(st, exp, desc) \

do { \

printf(desc); \

(st); \

assert(exp); \

printf("OK "); \

} while(0)

int main(int argc, char **argv) {

int ret;

int i;

short val;

double fir;

struct ring *ring;

printf("Testing Ring Buffer... ");

printf(" Testing null safety... ");

ASSERT(ret = ring_size(NULL), ret == -1, "Testing ring_size is null safe...");

ASSERT(ret = ring_enqueue(NULL, VAL), ret == -1, "Testing ring_enqueue is null safe...");

ASSERT(ret = ring_dequeue(NULL, &val), ret == -1, "Testing ring_dequeue is null safe...");

ASSERT(ret = ring_apply_fir(NULL, NULL), ret == 0, "Testing ring_apply_fir is null safe...");

ASSERT(ring_free(NULL), 1, "Testing ring_free is null safe...");

printf(" Testing basic functionality... ");

ASSERT(ring = ring_create(SIZE), ring != NULL, "Testing we can create a ring buffer...");

ASSERT(ret = ring_size(ring), ret == 0, "Testing size is 0...");

ASSERT(ret = ring_dequeue(ring, &val), ret == -1, "Testing we can't dequeue when empty...");

ASSERT(ret = ring_enqueue(ring, VAL), ret == 0, "Testing we can enqueue an item...");

ASSERT(ret = ring_dequeue(ring, &val), ret == 0 && val == VAL, "Testing we can dequeue an item...");

printf(" Filling up the buffer... ");

for (i = 0; i

ASSERT(ret = ring_enqueue(ring, VAL + i), ret == 0, "Testing we can enqueue an item...");

ASSERT(ret = ring_size(ring), ret == i + 1, "Testing size is correct...");

}

ASSERT(ret = ring_enqueue(ring, VAL + SIZE), ret == -1, "Testing we can't enqueue to a full buffer...");

printf(" Testing FIR Calculations... ");

double weights1[] = {1, 0, 0};

double weights2[] = {0, 1, 0};

double weights3[] = {0, 0, 1};

double weights4[] = {1, 1, 1};

ASSERT(fir = ring_apply_fir(ring, weights1), fir == (double)(VAL + 0), "Testing we can select the first item...");

ASSERT(fir = ring_apply_fir(ring, weights2), fir == (double)(VAL + 1), "Testing we can select the second item...");

ASSERT(fir = ring_apply_fir(ring, weights3), fir == (double)(VAL + 2), "Testing we can select the third item...");

ASSERT(fir = ring_apply_fir(ring, weights4), fir == (double)(3 * VAL + 3), "Testing we can sum all the items...");

printf(" Emptying the buffer... ");

for (i = 0; i

ASSERT(ret = ring_size(ring), ret == SIZE - i, "Testing size is correct...");

ASSERT(ret = ring_dequeue(ring, &val), ret == 0 && val == VAL + i, "Testing we can dequeue..."); }

ASSERT(ret = ring_dequeue(ring, &val), ret == -1, "Testing we can't dequeue when empty...");

ASSERT(ring_free(ring), 1, "Testing we can free the ring...");

printf(" All Ring Buffer tests passed! ");

}

The second concept we will examine in this assignment is the Finite Impulse Response filter (or FIR Filter). We will be focusing on the casual discrete time FIR filter. This means that the filter only depends on its current and past values, each of which occur at a distinct point in time. Imagine you have a finite buffer of the N most recent values coming from a sensor. However, you know that the sensor readings contain a lot of noise which you want to eliminate before processing. We can use a casual discrete time FIR filter to compute a weighted sum of the last N points (based on a known weighting for each reading). We will use this filter to compute a moving average. The formula for the filter is as follows: y[n] = box[n] + b x[n - 1] + ... +byx[n - N] = b; - x[n - i], N

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions

Question

Write short notes on departmentation.

Answered: 1 week ago

Question

What are the factors affecting organisation structure?

Answered: 1 week ago

Question

What are the features of Management?

Answered: 1 week ago

Question

Briefly explain the advantages of 'Management by Objectives'

Answered: 1 week ago