function [a,m] = dct(y)
% DCT calculates the discrete cosine transform of the input vector y
% [a m] = dct(y)
% y: input vector with length N
% a: the conside transform. This is from a_0 to a_{N-1}
% m: the index associated with a.
% Convecrt 1*N vector to N*1
if size(y,2) > size(y,1)
y = transpose(y);
end
N = length(y);
a = zeros(N-1,1);
for m=0:N-1
TEMP = sum( ...
1 / (N - 1) * ...
( ...
y(1:end-1) .* cos(pi * m * (0:N-2)' / (N - 1)) + ...
y(2:end) .* cos(pi * m * (1:N-1)' / (N - 1)) ...
));
if m == 0
a0 = TEMP;
else
a(m) = TEMP;
end
end
a = [a0;a];
m = (0:N-1)';
Cite As
Koorosh Gobal (2026). dct(y) (https://www.mathworks.com/matlabcentral/fileexchange/49825-dct-y), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
- Signal Processing > Signal Processing Toolbox > Transforms, Correlation, and Modeling > Transforms >
Tags
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
| Version | Published | Release Notes | |
|---|---|---|---|
| 1.0.0.0 |
