Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a Digital Signal processing course, it is given a .mat file containing a

ID: 3839662 • Letter: I

Question

I have a Digital Signal processing course, it is given a .mat file containing a touchtone sound array and a time array containing sound data for a series of button presses on a touchtone phone. The task is to implement a filter bank to automatically determine which buttons have been pressed AND at what time, for any arbitrary input touchtone signal.

1. Submit a Matlab code that: uses an input array ‘y’ and time array ‘t’ as the input touchtone signal, and returns 10 output arrays, ‘n1’…’n0’, each of which is the same length of the original

input signal and contains a “1” whenever its digit was pressed. For example, n5 should contain a “1” whenever a 5 is pressed and “0” otherwise. To achieve this, you will need to determine a threshold amplitude for the filter bank outputs and assign the a “1” to the array if the output signal is above that threshold value.

Explanation / Answer

Fish.c

#define _XOPEN_SOURCE 700
#include <stdlib.h> // Standard library
#include <time.h> // For seeding rand
#include <unistd.h> // For fork
#include <signal.h> // For signal
#include <stdbool.h> // For bool
#include <stdio.h> // Standard I/O
#include <sys/types.h> // For types (such as pid_t)
#include <sys/ipc.h> // For interprocess communication
#include <sys/shm.h> // For shared memory
#include <sys/wait.h> // For wait

void onInterrupt();
void onTermination();

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

   signal(SIGINT, onInterrupt);  
   signal(SIGTERM, onTermination);
  
   int sMemID; // Shared memory ID
   key_t keyValue = 1337;
   char (*dataPtr)[10][10]; // Pellet 2D Array
  
   // Create shared memory segment. Exit on error.
   if ((sMemID = shmget(keyValue, sizeof(char[10][10]), 0666)) < 0) {
       perror("Could not create memory segment! ");
       exit(1);
   }
  
   // Attach the shared memory segment. Exit on error.
   if ((dataPtr = (char(*)[10][10])shmat(sMemID, NULL, 0)) == (char(*)[10][10]) -1) {
       perror("Could not attach shared memory segment! ");
       exit(1);
   }

   *dataPtr[9][5] = 'F';
  
   while(1) {
       // Get location of the fish
       int fishCurrentCol;
       for (int i = 0; i < 10; i++) {
           if (*dataPtr[9][i] == 'F') {
               fishCurrentCol = i;
               //printf("Fish location: %d ", fishCurrentCol);
           }
       }
       // Check closest pellet row based on location of the fish
       int closestPelletRow = 10;
       for (int i = 9; i >= 0; i--) {
           for (int j = 9; j >= 0; j--) {
               if (*dataPtr[i][j] == 0x50 && abs(closestPelletRow - 9) < abs(i - 9)) {
                   // Pellet found!
                   closestPelletRow = i;
                   printf("Closest row: %d ", closestPelletRow);
               }
           }
       }
       // Check closest pellet column based on location of the fish
       int closestPelletCol = 10;  
       for (int i = 9; i >= 0; i--) {
           for (int j = 9; j >= 0; j--) {
               if (*dataPtr[i][j] == 0x50 && abs(closestPelletCol - fishCurrentCol) > abs(j - fishCurrentCol)) {
                   // Pellet found!
                   closestPelletCol = j;
                   printf("Closest column: %d ", closestPelletCol);
               }
           }
       }
       if (closestPelletCol != 10) {
           // Now move the fish!
           // Calculate distance
           int distanceCol = closestPelletCol - fishCurrentCol;
           while (abs(distanceCol)) {
               bool breakTestRow = false;
               // Check closest pellet row based on location of the fish
               int closestPelletRow = 10;
               for (int i = 9; i >= 0; i--) {
                   for (int j = 9; j >= 0; j--) {
                       if (*dataPtr[i][j] == 0x50 && abs(closestPelletRow - 9) < abs(i - 9)) {
                           break;
                           breakTestRow = true;
                       }
                       if (breakTestRow) {
                           break;
                       }
                   }
               }
               bool breakTestCol = false;
               // Check closest pellet column based on location of the fish
               int closestPelletCol = 10;  
               for (int i = 9; i >= 0; i--) {
                   for (int j = 9; j >= 0; j--) {
                       if (*dataPtr[i][j] == 0x50 && abs(closestPelletCol - fishCurrentCol) > abs(j - fishCurrentCol)) {
                           break;
                           breakTestCol = true;
                       }
                       if (breakTestCol) {
                           break;
                       }
                   }
               }
               *dataPtr[9][fishCurrentCol] = '~';
               if (distanceCol > 0) {
                   fishCurrentCol++;
               }
               else {
                   fishCurrentCol--;
               }
               *dataPtr[9][fishCurrentCol] = 'F';
               if (distanceCol > 0) {
                   distanceCol--;
               }
               else {
                   distanceCol++;
               }
               struct timespec s;
               s.tv_sec = 0;
               s.tv_nsec = 500000000;
               //nanosleep(&s, NULL);
               sleep(1);
           }
       }
       else {
           // Move the fish back to the middle!
           // Calculate distance
           int distanceCol = fishCurrentCol - 5;
           while (abs(distanceCol)) {
               bool breakTestRow = false;
               // Check closest pellet row based on location of the fish
               int closestPelletRow = 10;
               for (int i = 9; i >= 0; i--) {
                   for (int j = 9; j >= 0; j--) {
                       if (*dataPtr[i][j] == 0x50 && abs(closestPelletRow - 9) < abs(i - 9)) {
                           break;
                           breakTestRow = true;
                       }
                       if (breakTestRow) {
                           break;
                       }
                   }
               }
               bool breakTestCol = false;
               // Check closest pellet column based on location of the fish
               int closestPelletCol = 10;  
               for (int i = 9; i >= 0; i--) {
                   for (int j = 9; j >= 0; j--) {
                       if (*dataPtr[i][j] == 0x50 && abs(closestPelletCol - fishCurrentCol) > abs(j - fishCurrentCol)) {
                           break;
                           breakTestCol = true;
                       }
                       if (breakTestCol) {
                           break;
                       }
                   }
               }
               *dataPtr[9][fishCurrentCol] = '~';
               if (distanceCol > 0) {
                   fishCurrentCol--;
               }
               else {
                   fishCurrentCol++;
               }
               *dataPtr[9][fishCurrentCol] = 'F';
               if (distanceCol > 0) {
                   distanceCol--;
               }
               else {
                   distanceCol++;
               }
               struct timespec s;
               s.tv_sec = 0;
               s.tv_nsec = 500000000;
               //nanosleep(&s, NULL);
               sleep(1);
           }
       }
       sleep(1);
   }
   exit(0);
}

void onInterrupt() {
   fprintf(stderr, " (Fish) PID: %d - Died due to interrupt! ", getpid());
   exit(0);
}

void onTermination() {
   fprintf(stderr, " (Fish) PID: %d - Died due to termination! ", getpid());
   exit(0);

}

Pellet.c

#define _XOPEN_SOURCE 700
#include <stdlib.h> // Standard library
#include <time.h> // For seeding rand
#include <unistd.h> // For fork
#include <signal.h> // For signal
#include <stdbool.h> // For bool
#include <stdio.h> // Standard I/O
#include <sys/types.h> // For types (such as pid_t)
#include <sys/ipc.h> // For interprocess communication
#include <sys/shm.h> // For shared memory
#include <sys/wait.h> // For wait

void onInterrupt();
void onTermination();

bool eaten = false;  

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

   signal(SIGINT, onInterrupt);  
   signal(SIGTERM, onTermination);

   int sMemID; // Shared memory ID
   key_t keyValue = 1337;
   char (*dataPtr)[10][10]; // Pellet 2D Array
  
   // Create shared memory segment. Exit on error.
   if ((sMemID = shmget(keyValue, sizeof(char[10][10]), 0666)) < 0) {
       perror("Could not create memory segment! ");
       exit(1);
   }
  
   // Attach the shared memory segment. Exit on error.
   if ((dataPtr = (char(*)[10][10])shmat(sMemID, NULL, 0)) == (char(*)[10][10]) -1) {
       perror("Could not attach shared memory segment! ");
       exit(1);
   }
  
   srand(time(NULL));  
   int row = rand() % 10;
   int col = rand() % 10;
   while (*dataPtr[row][col] != '~') {
       row = rand() % 10;
       col = rand() % 10;  
   }
   *dataPtr[row][col] = 0x50; // My terminal doesn't support extended ASCII, so we won't be using 0x80.

   while(row < 9) {
       *dataPtr[row][col] = '~';
       row++;
       if (*dataPtr[row][col] == 'F') {
           *dataPtr[row][col] |= 0x50;
           eaten = true;
           sleep(1);
           break;
       }
       *dataPtr[row][col] = 0x50;
       sleep(1);
   }
   if (!eaten) {
       *dataPtr[row][col] = '~';
   }
   else if (*dataPtr[row][col] == 0x50) {
       *dataPtr[row][col] = 'F';
   }
   sleep(1);
   printf("Pellet PID: %d, X: %d, Y: %d - eaten: %s ", getpid(), row, col, eaten ? "true" : "false");
   exit(0);
}

void onInterrupt() {
   fprintf(stderr, " (Pellet) PID: %d - Died due to interrupt! - Eaten: %s ", getpid(), eaten ? "true" : "false");
   exit(0);
}

void onTermination() {
   fprintf(stderr, " (Fish) PID: %d - Died due to termination! - Eaten: %s ", getpid(), eaten ? "true" : "false");
   exit(0);

}


Swim_Mill.c

#define _XOPEN_SOURCE 700
#include <stdlib.h> // Standard library
#include <time.h> // For seeding rand
#include <unistd.h> // For fork
#include <signal.h> // For signal
#include <stdbool.h> // For bool
#include <stdio.h> // Standard I/O
#include <sys/types.h> // For types (such as pid_t)
#include <sys/ipc.h> // For interprocess communication
#include <sys/shm.h> // For shared memory
#include <sys/wait.h> // For wait

void onInterrupt();
void onTermination();

bool eaten = false;  

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

   signal(SIGINT, onInterrupt);  
   signal(SIGTERM, onTermination);

   int sMemID; // Shared memory ID
   key_t keyValue = 1337;
   char (*dataPtr)[10][10]; // Pellet 2D Array
  
   // Create shared memory segment. Exit on error.
   if ((sMemID = shmget(keyValue, sizeof(char[10][10]), 0666)) < 0) {
       perror("Could not create memory segment! ");
       exit(1);
   }
  
   // Attach the shared memory segment. Exit on error.
   if ((dataPtr = (char(*)[10][10])shmat(sMemID, NULL, 0)) == (char(*)[10][10]) -1) {
       perror("Could not attach shared memory segment! ");
       exit(1);
   }
  
   srand(time(NULL));  
   int row = rand() % 10;
   int col = rand() % 10;
   while (*dataPtr[row][col] != '~') {
       row = rand() % 10;
       col = rand() % 10;  
   }
   *dataPtr[row][col] = 0x50; // My terminal doesn't support extended ASCII, so we won't be using 0x80.

   while(row < 9) {
       *dataPtr[row][col] = '~';
       row++;
       if (*dataPtr[row][col] == 'F') {
           *dataPtr[row][col] |= 0x50;
           eaten = true;
           sleep(1);
           break;
       }
       *dataPtr[row][col] = 0x50;
       sleep(1);
   }
   if (!eaten) {
       *dataPtr[row][col] = '~';
   }
   else if (*dataPtr[row][col] == 0x50) {
       *dataPtr[row][col] = 'F';
   }
   sleep(1);
   printf("Pellet PID: %d, X: %d, Y: %d - eaten: %s ", getpid(), row, col, eaten ? "true" : "false");
   exit(0);
}

void onInterrupt() {
   fprintf(stderr, " (Pellet) PID: %d - Died due to interrupt! - Eaten: %s ", getpid(), eaten ? "true" : "false");
   exit(0);
}

void onTermination() {
   fprintf(stderr, " (Fish) PID: %d - Died due to termination! - Eaten: %s ", getpid(), eaten ? "true" : "false");
   exit(0);

}


makefile

all: Fish Pellet Swim_Mill
Fish: Fish.o
   gcc -o Fish Fish.o
Fish.o: Fish.c
   gcc -c Fish.c -o Fish.o -std=c99
Pellet: Pellet.o
   gcc -o Pellet Pellet.o
Pellet.o: Pellet.c
   gcc -c Pellet.c -o Pellet.o -std=c99
Swim_Mill: Swim_Mill.o
   gcc -o Swim_Mill Swim_Mill.o
Swim_Mill.o: Swim_Mill.c
   gcc -c Swim_Mill.c -o Swim_Mill.o -std=c99