If you want to get gradients wrt the input, you input should already be a Variable.
For the autograd to work properly, you have to only work with Variable (note that python numbers can be used as is without wrapping them in a Variable).
This means that your function will take  Variable as input, and should return a Variable and only perform operations with Variables inside.
If you need to create a constant matrix inside your function, you can create it as a Tensor, but before using it in an operation with your input (which is a Variable), this Tensor should be wrapped in a Variable. Keep in mind that when you do Variable(DCT_matrix), this is the same as Variable(DCT_matrix, requires_grad=False): you create a new constant in your graph that does not require gradients and thus we should not backpropagate to it.