How to fix a shift in frequency in MATLAB?

I am trying to simulate a nonideal zero-order hold, but I am getting an increase in frequency. Here is my code:

amp = 1;
f = 1e9;
fs = 10e9;
phase = 90;
phase = phase/180*pi;
Ts = 1/fs;
start = 0;
cycles = 10;
duration = cycles * Ts;
ts = start:Ts:duration;
vin = amp*cos(2*pi*f*ts + phase);
t = start:0.0001*Ts:duration;
v0 = 0;
vf = vin;
tau = 10e-12;
m = length(t);
n = length(ts);
sections = m/n;
vt = zeros(n,sections);
temp = vector2matrix(t,sections);
for ii = 1:1:n
    for jj = 1:1:sections
        vt(ii,jj) = vf(ii) + (v0 - vf(ii))*exp(-temp(ii,jj)/tau); %ZOH formula
    end
end
vt = vt';
vt = vt(:)';
figure;
plot(t,vt);%xlim([0 0.1e-9]);
hold on
stairs(ts,vf);
hold off

In the following image, I am getting the blue trace when it should look something like the orange trace:

enter image description here

Upvotes: 0

Views: 97

Answers (1)

ViG
ViG

Reputation: 1868

There isn't a frequency shift, it's because there are not enough samples to follow the blue line. The red curve has

ts =1.0e-09 *[0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000    0.7000    0.8000    0.9000    1.0000]

,so it has a lot less points to display the function compared to the blue curve. On these points the blue and red curve coincide, so they are correct. Another way to see this, is to add more points to ts. Consider the following code:

...
cycles = 12;
duration = cycles * Ts;
ts = start:.8*Ts:duration-.8*Ts;
vin = amp*cos(2*pi*f*ts + phase);
t = start:0.0001*Ts:duration-0.0001*Ts;
...
figure;
plot(t,vt,'k');%xlim([0 0.1e-9]);
hold on
stairs(ts,vf,'y--');
hold off 

All the code is the same except the number of cycles and the steps for ts (now 4 samples longer). The following graph is obtained:

Now the two nicely overlap, so you can see that there isn't a frequency shift.

Upvotes: 1

Related Questions