0

Here is my code:

clear all;
%% Load the earthquake data file
load ECE350_Earthquake_Demo.mat
tearth = 0:dt:(length(d)-1)*dt;
t1 = tearth';
%% Play the sound of the earthquake
sound(d, fs)
figure,subplot(3, 1, 1); % 3 subplots in a 3x1 matrix
plot(t1,d) %% plots f(t)
title('First Subplot f(t)')

subplot(3, 1, 2);
plot(t1*2, d) %% plots f(2t)
title('Second Subplot f(2t)')

subplot(3, 1, 3);
plot(t1*(1/2), d) %% plots f(t/2)
title('Third Subplot f(t/2)')

xlim([0 20]);
orient landscape
delete 'Demo_plot1.pdf'
print -dpdf 'Demo_plot1'

This code loads in an earthquake data file and plots the output onto a graph.

I am to plot three different subplots vertically, and plot f(t), f(2t), and f(t/2) respectively.

f(2t) should compress the graph, and f(t/2) should expand the graph, naturally. My code does the opposite - f(2t) compresses, and f(t/2) expands (t1*2 and t1/2 is how I am implementing this).

The output format is fine, and everything works. These two graphs are just switched.

Why is this?

user3006937
  • 49
  • 3
  • 10
  • 3
    `plot(t*2,d)` doesn't plot `f(2*t)`. Imagine `t=[1 2 3 4 5]`, then `plot(t,d)` would have x coordinates 1, 2, 3, 4, and 5. `plot(t*2,d)` would have x coordinates 2, 4, 6, 8, 10, and so would appear stretched out. – David Sep 02 '15 at 03:23
  • That's what I thought it might be doing. That was the only way I could think of to implement that. Would I have to maybe change something about the variable when I declare it? Rather than changing it in the subplots? – user3006937 Sep 02 '15 at 03:30
  • Just do it the opposite way to what you have been doing, i.e. `plot(t1/2,d)` for `f(2*t)` and vice versa. – David Sep 02 '15 at 03:32
  • Is that not just forcing it to look like what I want? Would that actually give me the real graph of f(2t) and f(t/2)? – user3006937 Sep 02 '15 at 03:34
  • Yes, it's the right thing to do. – David Sep 02 '15 at 03:43

1 Answers1

0

Here is a clean way to see that f(2t) really does compress functions in MATLAB, just like you think it should:

t = 0:.1:2*pi;
figure
hold on
plot(t, sin(t))
plot(t, sin(2*t))
plot(t, sin(t/2))
legend({'sin(t)', 'sin(2t)', 'sin(t/2)'})

In my example, this works nicely because sin is continuous: it can take any value of t as input. In your case, things are a bit more complicated because d is discrete: there is d(1), d(2), ..., but there is no d(.5).

If you "just want to see the right plots", let's think of d as being samples of a continuous function f, such that d(n) = f(dt * n) for integers n. Then for the plots, pick your ts so that you never need values of f between the ones you have:

t2 = t1 * 2;
plot(t2, d)

plots f(t/2) because when we plot the ith point, the t value is t2(i) = t1(i) * 2 and the f(t) value is d(i) = f(dt * i) = f(t1(i)).

t3 = t1 / 2;
plot(t3, d)

plots f(t * 2) because when we plot the ith point, the t value is t3(i) = t1(i) / 2 and the f(t) value is d(i) = f(dt * i) = f(t1(i)).

Tokkot
  • 1,215
  • 7
  • 22