Generate sequence of sine waves with changing amplitude
조회 수: 18 (최근 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
댓글 수: 2
Mathieu NOE
2021년 5월 3일
hi
You should share the code , not a picture if you want someone to help you
tx
채택된 답변
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
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
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!