Generate sequence of sine waves with changing amplitude

24 次查看(过去 30 天)
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

采纳的回答

VBBV
VBBV 2021-5-3
%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 个评论
VBBV
VBBV 2021-5-4
%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

请先登录,再进行评论。

更多回答(1 个)

DGM
DGM 2021-5-3
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 个评论
Michelle Weinmann
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 2021-5-3
They are all stored. They're just row vectors in one big array.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by