Question

1) Explain the IDFT function in Matlab 2) Re-write a SCILAB script that performs the IDFT. 3) Test your program using the fol

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A6p1.cpp

#include<bits/stdc++.h>
#include <sys/time.h>
#include <pthread.h>
using namespace std;

const int N=60;
char ar[N];
int n_threads; //stores no. of threads
int curr_thread=0; //stores index(0 to n_thread-1) of thread


// intitialize function intializes ar[] with random character values between 'A'-'Z'
void initialize(){

for(int i=0;i<N;i++){
ar[i]='A'+rand()%26; // rand()%26 will generate random integer between 0-25, ('A'+generated integer) will create character between A-Z
}
  
}


// print function prints the string in ar[]
void print(){
  
for(int i=0;i<60;++i){
cout<<ar[i];
}
  
}


// Each thread computes 1/p_thread th part of array
void* fun(void* arg){
  

// the part of array to be computed can be determined by the curr_thread(thread index)
int s=curr_thread*(N/n_threads),e=(curr_thread+1)*(N/n_threads);

for(int i=s;i<e;++i){
int x=ar[i]-'A';
x=25-x;
ar[i]=x+'a';
}
  
curr_thread++;
  
}

int main(int argc,char *argv[]){
  
n_threads=argv[1][0]-'0'; // get the no. of threads to be used from command line argument
  
cout<<"using "<<n_threads<<" threads\n\n";
  
initialize(); // initialize the character array


// print the original string
cout<<"original upper case string:\n";
print();
cout<<"\n\n";
  
pthread_t threads[n_threads];
  

// Creating threads
for(int i=0;i<n_threads;++i){
pthread_create(&threads[i],NULL,fun,(void *)NULL);
}
  

// joining threads i.e. waiting for all threads to complete
for(int i=0;i<n_threads;++i){
pthread_join(threads[i],NULL);
}
  

// print the final string
cout<<"complementary lower case string:\n";
print();
cout<<"\n\n";
  
}

A6p2.cpp

#include<bits/stdc++.h>
#include <sys/time.h>
#include <pthread.h>
using namespace std;

const int N=60; //no. of elements in array
const int r=5; //no. of rows
const int c=12; //no. of columns
int ar[r][c];
int n_threads; //stores no. of threads
int curr_thread=0; //stores index(0 to n_thread-1) of thread


// intitialize function intializes ar[] with random int values between 1-49
void initialize(){
  
srand(time(0));

for(int i=0;i<r;i++){
for(int j=0;j<c;++j){
ar[i][j]=(rand()%49)+1; // rand()%26 will generate random integer between 0-48, (1+generated integer) will create integer between 1-49
}
}
  
}


// print function prints the elements ofar[]
void print(){
  
for(int i=0;i<r;i++){
for(int j=0;j<c;++j){
cout<<ar[i][j]<<' ';
}
cout<<'\n';
}
  
}


// Each thread computes 1/p_thread th part of array
void* fun(void* arg){

// the part of array to be computed can be determined by the curr_thread(thread index)
int s=curr_thread*(N/n_threads),e=(curr_thread+1)*(N/n_threads);

for(int i=s;i<e;++i){
int r_i=i/c,c_i=i%c;
int x=ar[r_i][c_i];
ar[r_i][c_i]=x*x;
}
  
curr_thread++;
  
}

int main(int argc,char *argv[]){
  
n_threads=argv[1][0]-'0'; // get the no. of threads to be used from command line argument
  
cout<<"using "<<n_threads<<" threads\n\n";
  
initialize(); // initialize the array
  

// print the original array
cout<<"original array:\n";
print();
cout<<"\n\n";
  
pthread_t threads[n_threads];
  

// Creating threads
for(int i=0;i<n_threads;++i){
pthread_create(&threads[i],NULL,fun,(void *)NULL);
}
  

// joining threads i.e. waiting for all threads to complete
for(int i=0;i<n_threads;++i){
pthread_join(threads[i],NULL);
}
  

// print the final array
cout<<"updated array:\n";
print();
cout<<"\n\n";
  
}

make file(name of make file should be "Makefile")

all: A6p1 A6p2
A6p1: A6p1.cpp
g++ A6p1.cpp -l pthread -o A6p1
A6p2: A6p2.cpp
g++ A6p2.cpp -l pthread -o A6p27

Add a comment
Know the answer?
Add Answer to:
1) Explain the IDFT function in Matlab 2) Re-write a SCILAB script that performs the IDFT....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT