std::vector<double> fft(std::vector<double> signal) { float N = signal.size(); std::vector<std::complex<double>> dft(N); int32_t range = N/2; //range = 40; //int32_t range = 19; std::complex<double> j (0.0,1.0); for(int32_t k = -range; k<= range; k++) { for(uint32_t inc = 0; inc < N; inc++) { std::complex<double> tmp (k * (inc + 1) * 2 * PI / N, 0); std::complex<double> tmp2 (1.0/N * signal[inc], 0); dft[k + range] = dft[k + range] + tmp2 * pow(E_,-j * tmp); } } std::vector<double> final_result(N); for(uint32_t i = 0; i< N; ++i ) { final_result[i] = abs(dft[i]); } return final_result; }
int main(int argc, char *argv[]) { int tdcount, tlimit, mlimit; char exename[1024], inputfile[1024]; struct rlimit r; if (argc < 6) { printf("Usage: [id] [probid] [input] [time limit] [memory limit]\n"); exit(RET_SE); } tlimit = atoi(argv[4]); mlimit = atoi(argv[5]); sprintf(exename, "./%s", argv[1]); strcpy(inputfile, argv[3]); if ((pid = fork()) == 0) { freopen("input.txt", "r", stdin); chdir("sandbox"); chroot("."); freopen("output.txt", "w", stdout); setregid(99, 99); setreuid(99, 99); ptrace(PTRACE_TRACEME, 0, NULL, NULL); execl(exename, exename, NULL); exit(0); } signal(SIGALRM, timer); alarm(1); int stat, tmpmem, sig; for (;;) { wait4(pid, &stat, 0, &rinfo); if (WIFEXITED(stat)) { puts("exited!\n"); break; } else if (WIFSTOPPED(stat)) { sig = WSTOPSIG(stat); if (sig == SIGTRAP) { if (checkSyscall() == RET_RF) { ptrace(PTRACE_KILL, pid, NULL, NULL); final_result(RET_RF); } } else if (sig == SIGUSR1) { } else printf("Stopped due to signal: %d\n", sig); } else if (WIFSIGNALED(stat)) { //Runtime Error printf("Runtime Error. Received signal: %d\n", WTERMSIG(stat)); final_result(RET_RE); break; } tmpmem = getMemory(); if (tmpmem > maxmem) maxmem = tmpmem; if (maxmem > mlimit) final_result(RET_MLE); if (getRuntime() > tlimit) { ptrace(PTRACE_KILL, pid, NULL, NULL); final_result(RET_TLE); } ptrace(PTRACE_SYSCALL, pid, NULL, NULL); } final_result(RET_AC); return 0; }