There are a few issues with your main signal processing loop.
for (i=0; i < itera; i++) {
y[0] = x[i];
aux=0;
for (n=0; n<m; n++){
aux += coef[n] * y[n];
}
saida[i]=aux;
for (n=m; n <2; n--){
x[n] = x[n-1];
}
}
Every element of your coef array is the same, so you may as well make it a single constant value of 1.0 / m. For the y array, you are not setting any of its elements apart from the first. Thus your loop adds a constant multiplied by an uninitialized value 16 times over to aux. This is what produces the garbage output. I'm not sure what your (n=m; n <2; n--) loop is supposed to do, but it will never even run since m > 2.
A (inefficient) simple moving average would look something more like this:
for (i = 0; i < itera - m; i++) {
aux = 0;
for (n = 0; n < m; n++){
aux += x[i+n];
}
saida[i] = aux * (1.0 / m);
}
A more efficient version could avoid repeatedly processing intermediate elements, and instead just add the new number entering the sliding window and subtract the old number leaving the window each iteration. If dealing with floating point numbers, care would have to be taken with numerical stability when handling ill conditioned numbers etc, but that's a whole different problem that you don't have to concern yourself about for now.
It's important to distinguish between the file descriptor, which is a small integer that the process uses in its read and write calls to identify the file, and the file description, which is a structure in the kernel. The file offset is part of the file description. It lives in the kernel.
As an example, let's use this program:
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
int main(void)
{
int fd;
fd = open("output", O_CREAT|O_TRUNC|O_WRONLY, 0666);
if(!fork()) {
/* child */
write(fd, "hello ", 6);
_exit(0);
} else {
/* parent */
int status;
wait(&status);
write(fd, "world\n", 6);
}
}
(All error checking has been omitted)
If we compile this program, call it hello
, and run it like this:
./hello
here's what happens:
The program opens the output
file, creating it if it didn't exist already or truncating it to zero size if it did exist. The kernel creates a file description (in the Linux kernel this is a struct file
) and associates it with a file descriptor for the calling process (the lowest non-negative integer not already in use in that process's file descriptor table). The file descriptor is returned and assigned to fd
in the program. For the sake of argument suppose that fd
is 3.
The program does a fork(). The new child process gets a copy of its parent's file descriptor table, but the file description is not copied. Entry number 3 in both processes' file tables points to the same struct file
.
The parent process waits while the child process writes. The child's write causes the first half of "hello world\n"
to be stored in the file, and advances the file offset by 6. The file offset is in the struct file
!
The child exits, the parent's wait()
finishes, and the parent writes, using fd 3 which is still associated with the same file description that had its file offset updated by the child's write()
. So the second half of the message is stored after the first part, not overwriting it as it would have done if the parent had a file offset of zero, which would be the case if the file description was not shared.
Finally the parent exits, and the kernel sees that the struct file
is no longer in use and frees it.
Best Answer
In simple words, when you open a file, the operating system creates an entry to represent that file and store the information about that opened file. So if there are 100 files opened in your OS then there will be 100 entries in OS (somewhere in kernel). These entries are represented by integers like (...100, 101, 102....). This entry number is the file descriptor. So it is just an integer number that uniquely represents an opened file for the process. If your process opens 10 files then your Process table will have 10 entries for file descriptors.
Similarly, when you open a network socket, it is also represented by an integer and it is called Socket Descriptor. I hope you understand.