What you might be forgetting is that
dwt downsamples. And
db2 is a quite poor filter. So if you
idwt either approx or details, by replacing the other by zeros, you almost get your coefficient in order: on the top, the original signal, the two low frequencies in the second plot, the high frequencies in the third plot. Of course you have some aliasing, due to the poor resolution.
Try the attached code with
db24, you will get much less artifact.
What happens now with the approximation and the details? My interpretation is that the approximation is now sampled at $500$ kHz, and the details too, except that is now aliased to baseband:
and they need to be unmixed by reciprocal filters of the
FFTR.m code should be downloaded.
But why study sines with discrete wavelets?
f = [20/1000 100/1000 300/1000];
nSample = 1024; time = (1:nSample)';
data = cos(2*pi*time*f);
dataSum = sum(data,2);
waveName = 'db2';
[a,d] = dwt(dataSum,waveName);
z = zeros(size(a));
dataSumD = idwt(z,d,waveName);
dataSumA = idwt(a,z,waveName);
timeAD = 1:2*length(a);
So you have the code, I do not know whether you did stuff correctly, and wavelets are quite useful if used widely on signals that are worth it. Sines typically are not on the list.