-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdft.cpp
More file actions
81 lines (75 loc) · 2.13 KB
/
Copy pathdft.cpp
File metadata and controls
81 lines (75 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by Naveen on 17-12-2021.
//
#include <iostream>
#include <cmath>
#include<iomanip>
#define PI 3.14159265
using namespace std;
class calc{
int N;
protected:float w,c[16],s[16],cDft[16],sDft[16];
public:
//Constructor to initialize length N
calc(int x){
N=x;
cout<<"Parent Constructor called 2"<<endl;
}
calc(){
cout<<"Parent Constructor called 1"<<endl;
}
//Destructor to print message
~calc(){
cout<<"Destructor called"<<endl;
}
//setting the real part co-efficients
void setC(int n,float cn[]){
for(int i=0;i<n;i++){
c[i]=cn[i];
}
}
//Setting the imaginary part co-efficients
void setS(int n,float sn[]){
for(int i=0;i<n;i++){
s[i]=sn[i];
}
}
friend class idft; //friend class IDFT
};
class dft: public calc{
public:
dft(int x): calc(){
cout<<"Child Constructor called"<<endl;
}
void calcDFT(int N){
for(int k=0;k<N;k++){
cDft[k]=0;
sDft[k]=0;
for(int j=0;j<N;j++){
w=(2*PI*k*j)/N;
cDft[k]+= (c[j] * cos(w) + s[j] * sin(w));
sDft[k]+=(s[j] * cos(w) - c[j] * sin(w) );
}
cout<<setprecision(2);
cout<<fixed<<cDft[k]<<" "<<fixed<<sDft[k]<<"i"<<endl;
}
}
};
class idft{
public:
void calcIDFT(calc &idft) {
for (int k = 0; k < idft.N; k++) {
idft.cDft[k]=0;
idft.sDft[k]=0;
for (int n = 0; n < idft.N; n++) {
idft.w = (-1) * (2 * PI * k * n) / idft.N;
idft.cDft[k] += (idft.c[n] * cos(idft.w) + idft.s[n] *sin(idft.w));
idft.sDft[k] += (idft.c[n]*sin(idft.w) - idft.s[n] * cos(idft.w)) ;
}
idft.cDft[k] /= idft.N;
idft.sDft[k] /= idft.N;
cout<<setprecision(2);
cout << fixed<< idft.cDft[k] << " " <<fixed <<-idft.sDft[k]<<"i"<< endl;
}
}
};