import java.util.*;
class ConcentricMatrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int x1Length,x2Length,i=0,j=0;
// First Sequence
System.out.println("Enter the length of first sequence");
x1Length = sc.nextInt();
int x1[][]=new int[x1Length][x1Length];
int x2[]=new int[x1Length];
int y[]=new int[x1Length];
System.out.println("Enter first sequence");
for(i=0;i<x1Length;i++)
x1[0][i] = sc.nextInt();
// Second Sequence
System.out.println("Enter the length of second sequence");
x2Length = sc.nextInt();
System.out.println("Enter second sequence");
for(i=0;i<x2Length;i++)
x2[i] = sc.nextInt();
// Padding
if(x1Length>x2Length)
{
for(i=x1Length;i<x2Length;i++)
x2[i]=0;
}
// Circular Matrix
for(i=1;i<x1Length;i++)
{
for(j=0;j<x1Length;j++)
{
if(j-1<0)
x1[i][j]=x1[i-1][x1Length-1];
else
x1[i][j]=x1[i-1][j-1];
}
}
// Print Circular Matrix
System.out.println("\nCircular Matrix");
for(i=0;i<x1Length;i++)
{
for(j=0;j<x1Length;j++)
{
System.out.print(x1[j][i] + "\t");
}
System.out.println("");
}
// Perform Circular Matrix
for(i=0;i<x1Length;i++)
{
for(j=0;j<x1Length;j++)
{
y[i]+=(x1[j][i]*x2[j]);
}
}
// Print Output
System.out.println("\nCircular Convolution - y(n)");
for(i=0;i<x1Length;i++)
{
System.out.print(y[i] + "\t");
}
}
}
OUTPUT:
Enter the length of first sequence
4
Enter first sequence
1
2
3
4
Enter the length of second sequence
3
Enter second sequence
7
0
3
Circular Matrix
1 4 3 2
2 1 4 3
3 2 1 4
4 3 2 1
Circular Convolution - y(n)
16 26 24 34
class ConcentricMatrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int x1Length,x2Length,i=0,j=0;
// First Sequence
System.out.println("Enter the length of first sequence");
x1Length = sc.nextInt();
int x1[][]=new int[x1Length][x1Length];
int x2[]=new int[x1Length];
int y[]=new int[x1Length];
System.out.println("Enter first sequence");
for(i=0;i<x1Length;i++)
x1[0][i] = sc.nextInt();
// Second Sequence
System.out.println("Enter the length of second sequence");
x2Length = sc.nextInt();
System.out.println("Enter second sequence");
for(i=0;i<x2Length;i++)
x2[i] = sc.nextInt();
// Padding
if(x1Length>x2Length)
{
for(i=x1Length;i<x2Length;i++)
x2[i]=0;
}
// Circular Matrix
for(i=1;i<x1Length;i++)
{
for(j=0;j<x1Length;j++)
{
if(j-1<0)
x1[i][j]=x1[i-1][x1Length-1];
else
x1[i][j]=x1[i-1][j-1];
}
}
// Print Circular Matrix
System.out.println("\nCircular Matrix");
for(i=0;i<x1Length;i++)
{
for(j=0;j<x1Length;j++)
{
System.out.print(x1[j][i] + "\t");
}
System.out.println("");
}
// Perform Circular Matrix
for(i=0;i<x1Length;i++)
{
for(j=0;j<x1Length;j++)
{
y[i]+=(x1[j][i]*x2[j]);
}
}
// Print Output
System.out.println("\nCircular Convolution - y(n)");
for(i=0;i<x1Length;i++)
{
System.out.print(y[i] + "\t");
}
}
}
OUTPUT:
Enter the length of first sequence
4
Enter first sequence
1
2
3
4
Enter the length of second sequence
3
Enter second sequence
7
0
3
Circular Matrix
1 4 3 2
2 1 4 3
3 2 1 4
4 3 2 1
Circular Convolution - y(n)
16 26 24 34
No comments:
Post a Comment