I'm trying to implement a 1 dimensional DFT without using Matlab built-in functions such as fft(). This is my code
function [Xk] = dft1(xn)
N=length(xn);
n = 0:1:N-1; % row vector for n
k = 0:1:N-1; % row vecor for k
WN = exp(-1j*2*pi/N); % Twiddle factor (w)
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ nk; % DFT matrix
Xk = (WNnk*xn );
when i run the code after using the following commands:
I = imread('sample.jpg')
R = dft1(I)
I get this particular error: Error using * MTIMES is not fully supported for integer classes. At least one input must be scalar. To compute elementwise TIMES, use TIMES (.*) instead.
Can someone please help me to figure out how to solve this problem
Note: I am still in the very beginning level of learning Matlab thank you very much
You just need to cast the data to
double
, then run your code again. Basically what the error is saying is that you are trying to mix classes of data together when applying a matrix multiplication between two variable. Specifically, the numerical vectors and matrices you define indft1
are all of adouble
type, yet your image is probably of typeuint8
when you read this in throughimread
. This is why you're getting that integer error becauseuint8
is an integer class and you are trying to perform matrix multiplication with this data type with those of adouble
data type. Bear in mind that you can mix data types, so long as one number is a single number / scalar. This is also what the error is alluding to. Matrix multiplication of varaibles that are not floating point (double
,single
) is not supported in MATLAB so you need to make sure that your image data and your DFT matrices are the same type before applying your algorithm.As such, simply do:
Minor Note
This code is quite clever, and it (by default) applies the 1D DFT to all columns of your image. The output will be a matrix of the same size as
I
where each column is the 1D DFT result of each column fromI
.This is something to think about, but should you want to apply this to all rows of your image, you would simply transpose
I
before it goes intodft1
so that the rows become columns and you can operate on these new "columns". Once you're done, you simply have to transpose the result back so that you'll get your output fromdft1
shaped such that the results are applied on a per row basis. Therefore:Hope this helps! Good luck!