Generate sequence of sine waves with changing amplitude

19 views (last 30 days)
I'm trying to create a series of 11 sine functions with a changing amplitude. The 11 amplitudes are stored in a 1x11 array. The script is supposed to subtract two signals and generate a third. It's working until the section that assigns the signals (V1sig etc), but I just can't get the sine wave assignment correct; I keep getting errors about indexing or array sizing. I've tried a couple different ways with no success, hence why V1sig and V3sig are different. Any help would be appreciated, I'm sure I'm overlooking something simple.
f = 2.36e3;
w = 2*pi*f;
%sampling frequency
fs = 50000;
dt = 1/fs;
%Amplitude of pickup signal at each of 11 data points, and phase
Amp = [0.0654 0.0546 0.0436 0.0326 0.0219 0.0127 0.0099 0.0171 0.0272 0.0380 0.0482];
phase1 = -42*(pi/180);
%Amplitude of background signal, and phase
A2 = 0.2;
phase2 = 82*(pi/180);
%Timescale for plotting
t = 0:dt:(pi/2);
for k = 1:11
A1(k) = Amp(k);
V1(k)= A1(k)*exp(1i*(-phase1));
V2= A2*exp(1i*(-phase2));
V3(k)= V1(k)-V2;
R = real(V3);
I = imag(V3);
r = sqrt(R.^2 + I.^2);
theta(k) = atan(I/R);
V1sig = A1.*cos(w*t+phase1);
V2sig = A2*cos(w*t+phase2);
V3sig(k) = r(k).*cos(w*t+theta(k));
figure
plot(t,V1sig,t,V2sig,t,V3sig);
axis([0 pi/2 -0.5 0.5]);
legend('V1','V2','V3');
end
  2 Comments
Mathieu NOE
Mathieu NOE on 3 May 2021
hi
You should share the code , not a picture if you want someone to help you
tx

Sign in to comment.

Accepted Answer

VBBV
VBBV on 3 May 2021
%f true
f = 2.36e3;
w = 2*pi*f;
%sampling frequency
fs = 50000;
dt = 1/fs;
%Amplitude of pickup signal at each of 11 data points, and phase
Amp = [0.0654 0.0546 0.0436 0.0326 0.0219 0.0127 0.0099 0.0171 0.0272 0.0380 0.0482];
phase1 = -42*(pi/180);
%Amplitude of background signal, and phase
A2 = 0.2;
phase2 = 82*(pi/180);
%Timescale for plotting
t = 0:dt:(pi/2);
for k = 1:length(Amp)
A1(k) = Amp(k);
V1(k)= A1(k)*exp(1i*(-phase1));
V2= A2*exp(1i*(-phase2));
V3(k)= V1(k)-V2;
R = real(V3(k));
I = imag(V3(k));
r = sqrt(R.^2 + I.^2);
theta(k) = atan(I/R);
V1sig(:,k) = A1(k)*cos(w*t+phase1);
V2sig(:,k) = A2*cos(w*t+phase2);
V3sig(:,k) = r*cos(w*t+theta(k));
%figure
plot(t(1:500:end),V1sig(1:500:end,k),t(1:500:end),V2sig(1:500:end,k),t(1:500:end),V3sig(1:500:end,k));
axis([0 pi/2 -0.5 0.5]);
legend('V1','V2','V3');
end
Try this
  1 Comment
VBBV
VBBV on 4 May 2021
%f true
f = 2.36e3;
w = 2*pi*f;
%sampling frequency
fs = 50000;
dt = 1/fs;
%Amplitude of pickup signal at each of 11 data points, and phase
Amp = [0.0654 0.0546 0.0436 0.0326 0.0219 0.0127 0.0099 0.0171 0.0272 0.0380 0.0482];
phase1 = -42*(pi/180);
%Amplitude of background signal, and phase
A2 = 0.2;
phase2 = 82*(pi/180);
%Timescale for plotting
t = 0:dt:(pi/2);
for k = 1:length(Amp)
A1(k) = Amp(k);
V1(k)= A1(k)*exp(1i*(-phase1));
V2= A2*exp(1i*(-phase2));
V3(k)= V1(k)-V2;
R = real(V3(k));
I = imag(V3(k));
r = sqrt(R.^2 + I.^2);
theta(k) = atan(I/R);
V1sig(:,k) = A1(k)*cos(w*t+phase1);
V2sig(:,k) = A2*cos(w*t+phase2);
V3sig(:,k) = r*cos(w*t+theta(k));
%figure
plot(t(1:1000:end),V1sig(1:1000:end,k),t(1:1000:end),V2sig(1:1000:end,k),t(1:1000:end),V3sig(1:1000:end,k));
axis([0 pi/2 -0.5 0.5]);
legend('V1','V2','V3');
end

Sign in to comment.

More Answers (1)

DGM
DGM on 3 May 2021
f = 2.36e3;
w = 2*pi*f;
%sampling frequency
fs = 50000;
dt = 1/fs;
%Amplitude of pickup signal at each of 11 data points, and phase
A1 = [0.0654 0.0546 0.0436 0.0326 0.0219 0.0127 0.0099 0.0171 0.0272 0.0380 0.0482]';
phase1 = -42*(pi/180);
%Amplitude of background signal, and phase
A2 = 0.2;
phase2 = 82*(pi/180);
%Timescale for plotting
t = 0:dt:(pi/2);
V1= A1*exp(1i*(-phase1));
V2= A2*exp(1i*(-phase2));
V3= V1-V2;
r = abs(V3);
theta = angle(V3);
V1sig = A1.*cos(w*t+phase1);
V2sig = A2*cos(w*t+phase2);
V3sig = r.*cos(w*t+theta);
% i'm only plotting a small section so that there's something to see
nps = 100;
for p = 1:numel(A1)
subplot(3,4,p)
plot(t(1:nps),V1sig(p,1:nps),t(1:nps),V2sig(1:nps),t(1:nps),V3sig(p,1:nps));
legend('V1','V2','V3');
end
No guarantees that I didn't change anything in the edit, but I think that's the general idea. Just transpose the A vector and use implicit expansion to calculate all the signals as row vectors in an array.
  2 Comments
Michelle Weinmann
Michelle Weinmann on 3 May 2021
Thank you, the abs and angle functions cleaned it up nicely. I need to store each signal though so I kept it in the loop.
DGM
DGM on 3 May 2021
They are all stored. They're just row vectors in one big array.

Sign in to comment.

Categories

Find more on Simulink in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!