1

I have a stock market data set with the following two fields:

  • date
  • closing_price

I would like to apply DWT on this data set and plot a graph using Matlab. Can some one please guide me how to do it..

Thank you

EDIT

here is an example set of data

Date,Open,High,Low,Close,Volume

24-Oct-03,29.12,29.2,28.93,29.16,3069600

23-Oct-03,29.5,29.69,29.34,29.55,3414200

22-Oct-03,29.95,29.95,29.49,29.65,6659700

It is CSV.

I need to plot a DWT graph where x axis is date and y axis is any one or all of the variables.

rakesh kashyap
  • 1,418
  • 22
  • 41

1 Answers1

3

Computing DWT

This is actually straightforward. Use dwt to obtain the approximation and detail coefficients. For instance, a DWT using a daubechies 4-tap wavelet would be:

[cA, cD] = dwt(X, 'db4')

where X is your data.

If each sample in X has several fields, and you want to apply the DWT just on a certain field, say X.closing_price, add square brackets:

[cA, cD] = dwt([X.closing_price], 'db4')

Note that this solution assumes that the data samples are taken at constant time intervals.
To plot the data, you need to prepare another vector, which corresponds to the day number:

t = 1:2:length(X);

The x-axis values skips every other day because the approximation and detail coefficients vectors each have half of the samples of X.


Demo code

The following code generates random data and puts it into an array of structs, each element having two fields: date and closing_price:

%# Generate some random data
C = cell(31, 2);
C(:, 1) = arrayfun(@(z)[num2str(z), '-Oct-03'], 1:length(C), 'Un', 0);
C(:, 2) = num2cell(100 * randn(1, length(C)));
X = cell2struct(C, {'date', 'closing_price'}, 2);

Now, to business:

%# Apply DWT
[cA, cD] = dwt([X.closing_price], 'db4');

%# Prepare x-axis values
t = 1:2:length(X);

%# Plot result with respect to date
figure
subplot(2, 1, 1), plot(t, cA)
title('Approximation coefficients'), xlabel('day'), ylabel('C_A')
subplot(2, 1, 2), plot(t, cD)
title('Detail coefficients'), xlabel('day'), ylabel('C_D')

This is what you should get:

Expected result

Hope that helps!

Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • I am getting an error while plotting the graph.it says "Error using plot. Vectors must be the same lengths." also is it possible to have the date value itself in the x axis (I meant not changing them into 1 2 3 4 etc)... – rakesh kashyap Jul 31 '12 at 23:17
  • size(cA)=size(cD)=19 and size(t)=31 .... after applying [cA, cD] = dwt([X.closing_price], 'db4'); cA and cD counts will become lesser than the count of input data... hence I am getting the error "Vectors must be the same lengths"... – rakesh kashyap Aug 01 '12 at 16:44
  • Thanks a lot for the solution. So, will I not be able to plot for each day? – rakesh kashyap Aug 01 '12 at 17:34
  • In short, no. But I mislead you a little, I set the x-axis values to be from 1 to 19 to avoid the error. In reality, the x-axis values should be `1:2:length(X)`, skipping every other day. I still find it weird that `length(X)` is 31, but `cA` and `cD` are 19 samples each. They are supposed to have half the samples of `X`, each. Please note that I corrected the solution, but you should look into your data and verify that. – Eitan T Aug 01 '12 at 20:30
  • I wanted to know one more thing.. Is it possible to use whos and get the amount of memory(RAM) used by a function while finding DWT – rakesh kashyap Aug 15 '12 at 11:22
  • Well, you can put `whos('cA', 'cD')` after the `dwt` command to see how much memory they take. Is that what you mean? – Eitan T Aug 15 '12 at 11:48