How to obtain all the complex values of a R2C fftw - c

I'm using the fftw lib to compute a 2d r2c fit, but this function gives just n* (n/2+1) elements and I need all the values is there a way to obtain all the n*n elements? Thanks a lot. This is my code :
#include <stdio.h>
#include<Windows.h>
#include <fftw3.h>
#include<time.h>
#include<math.h>
void fourierForward(float *in,fftwf_complex *Out,int N)
{
int i,err;
fftwf_plan plan,planInv;
/*on calcule le plan d'exécution*/
plan=fftwf_plan_dft_r2c_2d ( N, N, in, Out, FFTW_ESTIMATE );
//planInv=fftwf_plan_dft_c2r_2d ( N, N, Out,out, FFTW_ESTIMATE );
fftwf_execute(plan);
//fftwf_execute(planInv);
fftwf_destroy_plan(plan);
//fftwf_destroy_plan(planInv);
//fftwf_free(Out);
}
int main()
{
const int n=500;
int i,j,k,l;
fftwf_complex *Out= (fftwf_complex*) fftwf_malloc((n/2+1)*n*sizeof(fftwf_complex));
FILE *f;
float fft[500][500];
float *in=(float*) fftwf_malloc(sizeof(float)*n*n);
FILE *pf=fopen("im.txt","r");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
fscanf(pf,"%f ",&in[i*n+j]);
}
}
for (i = 0; i < n*(n/2+1); i++)
{
Out[i][0]=0.0f;
Out[i][1]=0.0f;
}
fclose(pf);
fourierForward(in,Out,n);
f=fopen("RealPart.txt","w");
for(i=0;i<n;i++)
{
for(j=0;j<n/2+1;j++)
{
fft[i][j]=Out[i*(n/2+1)+j][0];
}
}
for(i=1;i<=(n-1)/2;i++)
{
fft[0][n-i]=fft[0][i];
}
for(i=1;i<n/2+1;i++)
{
for(j=1;j<n;j++)
{
fft[n-j][n-i]=fft[j][i];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
fprintf(f,"%f ",fft[i][j]);
}
fprintf(f,"\n");
}
system("pause");
fftwf_free(Out);
fclose(f);
return 0;
}
Best regards.

Related

why this show() function produce two * and the element will stop sometimes .There is no error in the code

This is a plane game function I wrote. I use a two-dimensional array to represent the game variables but The running result is abnormal, and * will jump suddenly.
And there will be two * , at the same time, and the plane also will stop
There should be no two * in the process of traversing the two-dimensional array. I tried to modify the position of * but I still couldn't.It's OK to run part of the code alone, but when you use the key operation, the program makes an error, but I don't know what's wrong
#include<stdio.h>
#include <windows.h>
#include<stdlib.h>
#include<conio.h>
#define enemynum 3
int element [20][30];
int position_x,position_y;
int enemy_x[enemynum],enemy_y[enemynum];
int score;
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void startup()
{ element[20][30]={0};
position_x=10;position_y=15;
element[position_x][position_y]=1;
for(int k=0;k<enemynum;k++)
{
enemy_x[k]=rand()%3;enemy_y[k]=rand()%20;
element[enemy_x[k]][enemy_y[k]]=3;
}
HideCursor();
}
This is an encapsulated pointer callback function. I don't think we need to consider the above functions
void show()
{ int i,j;
gotoxy(0,0);
for(i=0;i<20;i++)
{
for(j=0;j<30;j++)
{ if(element[i][j]==1)
{
printf("*");
}else if(element[i][j]==3)
{
printf("#");
}
else
printf(" ");
}
printf("\n");
}
}
void updatewhithout()
{
int i,j;
for(i=0;i<20;i++)
{
for(j=0;j<30;j++)
{
if(element[i][j]==2)
{
element[i][j]=0;
if(i>0)
element[i-1][j]=2;
}
}
} static int flag;
if(flag<20)
flag++;
if(flag==20)
{ for(int k=0;k<enemynum;k++)
{
if(enemy_x[k]==20)
{
enemy_x[k]=rand()%3;
enemy_y[k]=rand()%20;
}
element[enemy_x[k]][enemy_y[k]]=0;
enemy_x[k]++;
element[enemy_x[k]][enemy_y[k]]=3;
flag=0;
}
}
}
void updatewhith()
{ char ch;
if( kbhit())
ch=getch();
if(ch=='a')
{ element[position_x][position_y]=0;
position_y--;
element[position_x][position_y]=1;
}
if(ch=='d')
{ element[position_x][position_y]=0;
position_y++;
element[position_x][position_y]=1;
}
}
int main()
{startup();
while(1)
{show();
updatewhithout();
updatewhith();
}
}

Tried Knap snap problem but its not working

#include<stdio.h>
int max(int a,int b)
{
if(a>b)
return a;
return b;
}
void knacksnap(int n,int a[][n+1],int* val,int* weight,int maxweight)
{
if(n==0 || maxweight==0)
{
a[maxweight][n]=0;
return;
}
if(a[maxweight][n]!=-1)
return;
if(weight[n-1]>maxweight)
{
knacksnap(n-1,a,val,weight,maxweight);
a[maxweight][n]=a[maxweight][n-1];
}
else
{
knacksnap(n-1,a,val,weight,maxweight-weight[n-1]);
knacksnap(n-1,a,val,weight,maxweight);
a[maxweight][n]=max(val[n-1]+a[maxweight-weight[n-1]][n-1],a[maxweight][n-1]);
}
}
int main()
{
int n;
scanf("%d",&n);
int val[n],weight[n];
int i;
for(i=0;i<n;i++)
scanf("%d",&val[i]);
for(i=0;i<n;i++)
scanf("%d",&weight[i]);
int maxweight;
scanf("%d",&maxweight);
int a[maxweight+1][n+1];
int j;
for(i=0;i<maxweight+1;i++)
{
for(j=0;j<n+1;j++)
{
a[i][j]=-1;
}
}
knacksnap(n,a,val,weight,maxweight);
printf("\n");
printf("%d",a[maxweight][n]);
}
It not taking updated values by the knacksnap function. for example the inner knacksnap funtions are not generating correct values. eventhough their values are updated its not taking them. can someone please help

How can I draw a spline curves with c in codeblocks?

I am trying to draw a minimum enclosing circle and I need spline curves for points. but also I shouldn't use libraries. For preview, I used lines but they need curves. How can I fix it?
Which one is true arc, line or points?
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <conio.h>
struct nokta
{
int x,y;
};
//-------------------------------------------------------------//
int main()
{
int gd = DETECT;
int gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
initwindow(800,800);
struct nokta noktalar[50];
FILE *noktaal=fopen("noktalar.txt","r");
int i=0;
int sayac=0;
if(noktaal==NULL)
{
printf("Dosya yok");
}
while(!feof(noktaal))
{
fscanf(noktaal,"%d %d",&noktalar[i].x,&noktalar[i].y);
sayac++;
i++;
};
for(int i=0; i<sayac-1; i++)
{
printf("%d %d \n",noktalar[i].x,noktalar[i].y);
}
fclose(noktaal);
//----------------------------------------------------------------//
if(sayac==1)//If there is no point
{
printf("Lutfen koordinat giriniz.");
}
if(sayac==2)// If there is one point
{
printf("Lutfen birden fazla koordinat giriniz.");
}
if(sayac==3)//If there is two point
{
int uzunluk1=abs(noktalar[1].x-noktalar[0].x);
int uzunluk2=abs(noktalar[1].y-noktalar[0].y);
double yaricap=sqrt(pow(uzunluk1,2)+pow(uzunluk2,2));
float merkezx=(noktalar[1].x+noktalar[0].x)/2.0;
float merkezy=(noktalar[1].y+noktalar[0].y)/2.0;
printf("Yaricap:%.2f\n",yaricap/2);
printf("Merkez noktasi:{%.2f,%.2f}",merkezx,merkezy);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());//y düzlemi
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);//x düzlemi
putpixel(((getmaxx()/2)+(noktalar[0].x*20)),(getmaxy()/2)-(noktalar[0].y*20),GREEN);
putpixel(((getmaxx()/2)+(noktalar[1].x*20)),(getmaxy()/2)-(noktalar[1].y*20),GREEN);
for(int i=20; i<=800; i=i+20)
{
line(i,getmaxy()/2.03,i,getmaxy()/1.97);
}
for(int i=20; i<=800; i=i+20)
{
line(getmaxx()/2.03,i,getmaxx()/1.97,i);
}
circle((getmaxx()/2)+(merkezx*20),(getmaxy()/2)-(merkezy*20),yaricap*7.9);
getch();
closegraph();
}
if(sayac>3)//If there is three or more points.
{
int karetoplam;
double enbuyuk=1.0,yaricap1;
float merkez1x,merkez1y;
int sayac1=0;
int ikinoktauzakligi=0;
for(int i=0; i<sayac-1; i++)
{
for(int j=0; j<sayac-1; j++)
{
float ykare=pow(abs(noktalar[i].y-noktalar[j].y),2);
float xkare=pow(abs(noktalar[i].x-noktalar[j].x),2);
karetoplam=ykare+xkare;
if(enbuyuk<=karetoplam)
{
enbuyuk=karetoplam;
}
if(enbuyuk==karetoplam)
{
merkez1x=(noktalar[i].x+noktalar[j].x)/2.0;
merkez1y=(noktalar[i].y+noktalar[j].y)/2.0;
++sayac1;
}
}
}
if(sayac1>=2)
{
double cap1=sqrt(enbuyuk);
yaricap1=cap1/2;
printf("Ilk yari capi:%.2f\n",yaricap1);
printf("Ilk merkez noktasi:{%.2f,%.2f}\n",merkez1x,merkez1y);
line(getmaxx()/2,0,getmaxx()/2,getmaxy());//y düzlemi
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);//x düzlemi
for(int i=20; i<=800; i=i+20)
{
line(i,getmaxy()/2.03,i,getmaxy()/1.97);
}
for(int i=20; i<=800; i=i+20)
{
line(getmaxx()/2.03,i,getmaxx()/1.97,i);
}
for(int i=0; i<sayac-1; i++)
{
for(int j=0; j<sayac-1; j++)
{
putpixel((getmaxx()/2)+(noktalar[i].x*20),(getmaxy()/2)-(noktalar[i].y*20),GREEN);
}
}
float uzunluk2;
float arttir=0.0;
float enbuyuk2=0.0;
for(int i=0; i<100;i++)
{
for(int j=0; j<sayac-1; j++)
{
uzunluk2=sqrt((pow(noktalar[j].x,2)+pow(noktalar[j].y,2))-(pow(merkez1x,2)+pow(merkez1y,2)));
if(enbuyuk2<uzunluk2)
{
enbuyuk2=uzunluk2;
}
if(yaricap1<enbuyuk2)
{
yaricap1=yaricap1+0.1;
arttir=arttir+0.1;
}
}
}
printf("Son yari capi:%.2f\n",yaricap1);
printf("Son merkez noktasi:{%.2f,%.2f}",merkez1x+arttir,merkez1y+arttir);
int uzunluk3;
int enkucuk3=0;
for(i=0;i<sayac-1;i++)
{
line((getmaxx()/2)+(noktalar[i].x*20),((getmaxy()/2)+(noktalar[i].y*20)),((getmaxx()/2)+(noktalar[i+1].x*20)),((getmaxy()/2)+(noktalar[i+1].y*20)));
}
circle((getmaxx()/2)+((merkez1x+arttir)*20),(getmaxy()/2)-((merkez1y+arttir)*20),yaricap1*20);
getch();
closegraph();
}
}
}
//-------------------------------------------------------------------//
Minimum enclosing circle can be drawn with this but it doesn't show a curve. I need to show curves without libraries.

Strassen multiplication - c program

We had to implement strassen's multiplication in a practical session , the code I wrote is given below , as you can see I have used a lot of intermediate matrices .I wanted to know how to return a 2d array from a function so that the code will look cleaner and more understandable , also it will give me some insights on pointers(a weak area for me )i.e say is use a double pointer as return type of sub function (int **sub(args list))
and since my strassen function has prototype strassen(int , int [],int[]..)
When one argument of strassen function is a result of sub function , I get an error saying int (*)[] expected but returning int **
To resolve this I typecasted the result of sub function with int (*)[] but it does not work as expected
Help please ? Thanks !
#include<stdio.h>
#include<stdlib.h>
void add(int n,int a[n][n],int b[n][n],int result[][n])
{
printf("---add---\n");
int i,j;
//int **result = (int **)malloc(n*sizeof(int *));
/*for(i=0;i<n;i++)
result[i] = (int *)malloc(n*sizeof(int));*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
result[i][j] = a[i][j] + b[i][j];
printf("%d\t",result[i][j]);
}
printf("\n");
}
//return result;
}
void sub(int n,int a[n][n],int b[n][n],int result[][n])
{
printf("---sub---\n");
int i,j;
/*int **result = (int **)malloc(n*sizeof(int *));
for(i=0;i<n;i++)
result[i] = (int *)malloc(n*sizeof(int));*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
result[i][j] = a[i][j] - b[i][j];
printf("%d\t",result[i][j]);
}
}
}
void divide(int n,int a[n][n],int c[n/2][n/2],int i,int j)
{
int i1,i2,j1,j2;
for(i1=0,i2=i;i1<n/2;i1++,i2++)
{
for(j1=0,j2=j;j1<n/2;j1++,j2++)
{
c[i1][j1] = a[i2][j2];
}
}
}
void join(int n,int a[][n],int c[][n/2],int i,int j)
{
printf("join\n");
int i1,i2,j1,j2;
for(i1=0,i2=i;i1<(n/2);i1++,i2++)
{
for(j1=0,j2=j;j1<(n/2);j1++,j2++)
{
a[i2][j2] = c[i1][j1];
printf("c[%d][%d] %d\n",i1,j1,c[i1][j1]);
}
}
}
void multiply(int n,int a[][n],int b[][n],int result[][n])
{
int i,j;
if(n==2)
{
//partial products
printf("base case\n");
int p1 = (a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
int p2 = (a[1][0]+a[1][1])*b[0][0];
int p3 = a[0][0]*(b[0][1]-b[1][1]);
int p4 = a[1][1]*(b[1][0]-b[0][0]);
int p5 = (a[0][0]+a[0][1])*b[1][1];
int p6 = (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
int p7 = (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
int c11 = p1 + p4 - p5 + p7;
int c12 = p3 + p5;
int c21 = p2 + p4;
int c22 = p1 + p3 - p2 + p6;
result[0][0] = c11;
result[0][1] = c12;
result[1][0] = c21;
result[1][1] = c22;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",result[i][j]);
}
printf("\n");
}
}
else
{
int a11[n/2][n/2];
int a12[n/2][n/2];
int a21[n/2][n/2];
int a22[n/2][n/2];
int b11[n/2][n/2];
int b12[n/2][n/2];
int b21[n/2][n/2];
int b22[n/2][n/2];
//divide matrices A & B into four parts
divide(n,a,a11,0,0);
divide(n,a,a12,0,n/2);
divide(n,a,a21,n/2,0);
divide(n,a,a22,n/2,n/2);
divide(n,b,b11,0,0);
divide(n,b,b12,0,n/2);
divide(n,b,b21,n/2,0);
divide(n,b,b22,n/2,n/2);
//partial products
int p1[n/2][n/2],p2[n/2][n/2],p3[n/2][n/2],p4[n/2][n/2],p5[n/2][n/2],p6[n/2][n/2],p7[n/2][n/2];
int c11[n/2][n/2],c12[n/2][n/2],c21[n/2][n/2],c22[n/2][n/2];
int i1[n/2][n/2],i2[n/2][n/2];
add(n/2,a11,a22,i1);
add(n/2,b11,b22,i2);
multiply(n/2,i1,i2,p1);
int i3[n/2][n/2];
add(n/2,a21,a22,i3);
multiply(n/2,i3,b11,p2);
int i4[n/2][n/2];
sub(n/2,b12,b22,i4);
multiply(n/2,a11,i4,p3);
int i5[n/2][n/2];
sub(n/2,b21,b11,i5);
multiply(n/2,a22,i5,p4);
int i6[n/2][n/2];
add(n/2,a11,a12,i6);
multiply(n/2,i6,b22,p5);
int i7[n/2][n/2];
int i8[n/2][n/2];
sub(n/2,a21,a11,i7);
add(n/2,b11,b12,i8);
multiply(n/2,i7,i8,p6);
int i9[n/2][n/2];
int i10[n/2][n/2];
sub(n/2,a12,a22,i9);
add(n/2,b21,b22,i10);
multiply(n/2,i9,i10,p7);
//for c11
int r1[n/2][n/2];
int r2[n/2][n/2];
add(n/2,p1,p4,r1); //sub operation
sub(n/2,r1,p5,r2); //sub operation
add(n/2,r2,p7,c11); //main operation
//for c12
add(n/2,p3,p5,c12);
//for c21
add(n/2,p2,p4,c21);
//for c22
int r3[n/2][n/2];
int r4[n/2][n/2];
add(n/2,p1,p3,r3); //sub operation
sub(n/2,r3,p2,r4); //sub operation
add(n/2,r4,p6,c22); //main operation
join(n,result,c11,0,0);
join(n,result,c12,0,n/2);
join(n,result,c21,n/2,0);
join(n,result,c22,n/2,n/2);
printf("---c11---\n");
for(i=0;i<n/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("%d\t",c11[i][j]);
}
printf("\n");
}
printf("---c12---\n");
for(i=0;i<n/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("%d\t",c12[i][j]);
}
printf("\n");
}
printf("---c21---\n");
for(i=0;i<n/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("%d\t",c21[i][j]);
}
printf("\n");
}
printf("---c22---\n");
for(i=0;i<n/2;i++)
{
for(j=0;j<n/2;j++)
{
printf("%d\t",c22[i][j]);
}
printf("\n");
}
/*for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",result[i][j]);
}
printf("\n");
}*/
}
}
int main()
{
int n;
printf("Enter the order of the matrices(power of 2)\n");
scanf("%d",&n);
int i,j;
int a[n][n],b[n][n];
printf("Enter first matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter second matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("First matrix is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Second matrix is \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
int r[n][n];
multiply(n,a,b,r);
printf("---RESULT OF MULTIPLICATION---\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",r[i][j]);
}
printf("\n");
}
return 0;
}

Prime Generator(spoj)

For exact question see this link
Here I defined three function, called them selves one within another.
Function call is not being done
#include<stdio.h>
int primegen(int x1,int x2);
int isprime(int j);
int main(){
int x,n1,n2,i;
printf("Enter the number of test cases:");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("enter the starting point and ending point:");
scanf("%d %d",&n1,&n2);
primegen(n1,n2);
}
return 0;
}
int primegen(int x1,int x2){
int k;
if(x2>x1){
for(k=x1;k<x2;k++){
if(isprime(k))
{
printf("%d",k);
}
}
return 0;
}
}
int isprime(int j){
int i,c=0;
for (i=1;i<=j;i++)
{
if(j%i==0){
c++;
}
if(c!=2){
return 0;
}
else{
return 1;
}
}
}
Output
There is no output for this code.
Take following outside loop:
if(c!=2){
return 0;
}
else{
return 1;
}
The problem is in your loop in isprime().
In this you are using the value of c when it is still 0. So, c!=2 would result in true and 0 will be returned and you would not get any answer.
Remove this particular statement from the for loop as you need to calculate total no. of divisors.
for (i=1;i<=j;i++)
{
if(j%i==0)
c++;
}
if(c!=2)
return 0;
else
return 1;
Or you can do like this:
int isprime(int j){
int i,k;
k=sqrt(j); //include math.h for this
for(i=2;i<=k;i++)
{
if(j%i==0)
return 1;
}
return 0;
}
You only need to find any divisor up to the square root of the number.
It's better to return 0 if it is found to be divisible instead of using counter and incrementing it.
int isprime(int j)
{
int i;
for(i=2;i<j;i++)
if((j%i)==0)
return 0;
return 1;
}
#include <stdio.h>
#include <math.h>
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
unsigned int low,high,i=0,j=2,k,x=0,y=0,z;
unsigned long int a[200000],b[200000];
scanf("%d",&low);
scanf("%d",&high);
for(i=low;i<=high;i++)
a[x++]=i;
for(i=2;i<=32000;i++)
b[y++]=i;
i=0;
while(b[i]*b[i]<=high)
{
if(b[i]!=0)
{
k=i;
for(;k<y;k+=j)
{
if(k!=i)
{
b[k]=0;
}
}
}
i+=1;j+=1;
}
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=low && b[i]<=sqrt(high)))
printf("%d\n",b[i]);
}
int c=0;
for(i=0;i<y;i++)
{
if(b[i]!=0 && (b[i]>=1 && b[i]<=sqrt(high)))
b[c++]=b[i];
}
int m=a[0];
for(i=0;i<c;i++)
{
z=(m/b[i])*b[i];k=z-m;
if(k!=0)
k += b[i];
for(;k<x;)
{
if(a[k]!=0)
{
a[k]=0;
}
k+=b[i];
}
}
for(i=0;i<x;i++)
{
if(a[i]!=0 && (a[i]>=2 && a[i]<=(high)))
printf("%d\n",a[i]);
}
printf("\n");
}
return 0;
}

Resources