Problem in Addition of sine waves with different frequencies

15 vues (au cours des 30 derniers jours)
Asad Zubair
Asad Zubair le 15 Oct 2016
Commenté : Image Analyst le 16 Avr 2023
What is my mistake here? bcz this code is giving error
fs=1000;
f1=30;
f2=60;
t1=1/f1;
t2=1/f2;
ta=0:(1/fs):10*t1;
tb=0:(1/fs):10*t2;
A=sin(2*pi*f1*ta);
B=sin(2*pi*f2*tb);
subplot(211)
plot(ta,A)
subplot(212)
plot(tb,B)
n = min(min(ta) ,min(tb)) :max(max(ta) ,max(tb))
y1 = zeros (1,length(n))
y2=y1;
y1 (n>=min(ta)&n<=max(ta))=A
y2 (n>=min(tb)&n<=max(tb))=B
y = y1+y2;
ERROR:In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Untitled2 (line 20) y1 (n>=min(ta)&n<=max(ta))=A

Réponse acceptée

Image Analyst
Image Analyst le 16 Oct 2016
"I want to add two sine waves of 30 and 60 hz having sampling frequency of 1khz." <=== Try the code below:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Make 0.1 seconds sampled every 1/1000 of a second
t = 0 : 1/1000 : 0.1;
% Define sine wave parameters.
f1 = 30; % per second
T1 = 1/f1; % period, seconds
amp1 = 1; % amplitude
f2 = 60; % per second
T2 = 1/f2; % period, seconds
amp2 = 1; % amplitude
% Make signals.
signal1 = amp1 * sin(2*pi*t/T1);
signal2 = amp2 * sin(2*pi*t/T2);
signal = signal1 + signal2;
plot(t, signal1, 'r.-', 'LineWidth', 2, 'MarkerSize', 16);
hold on;
plot(t, signal2, 'm.-', 'LineWidth', 2, 'MarkerSize', 16);
plot(t, signal, 'b.-', 'LineWidth', 2, 'MarkerSize', 16);
grid on;
title('Sine Waves', 'FontSize', fontSize);
xlabel('Time', 'FontSize', fontSize);
ylabel('Signal', 'FontSize', fontSize);
% Make bolder x axis
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 3);
legend('30 Hz', '60 Hz', 'Sum');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
  2 commentaires
Asad Zubair
Asad Zubair le 16 Oct 2016
Thanks! Can you tell me if i take FFT of added signals(which you have shown above) how can i plot it with frequency at x-axis.
Image Analyst
Image Analyst le 16 Oct 2016
Modifié(e) : Image Analyst le 16 Oct 2016
I think each element of the fft array represents 1/(sampling time) = 1/(0.001) = 1000. So you could add this code:
% Take spectrum and plot it.
spectrum = fft(signal);
numElements = length(spectrum)
indexOf0 = numElements / 2
shiftedSpectrum = fftshift(abs(spectrum));
subplot(2, 1, 2);
f = (1 / sampleTime) * linspace(-numElements/2, numElements/2, numElements);
plot(f, shiftedSpectrum, 'r.-', 'LineWidth', 2, 'MarkerSize', 16);
grid on;
title('Spectrum of sum of 2 Sine Waves', 'FontSize', fontSize);
xlabel('Frequency', 'FontSize', fontSize);
ylabel('Power', 'FontSize', fontSize);
% Make bolder y axis
line([indexOf0, indexOf0], ylim, 'Color', 'k', 'LineWidth', 3);
I think I'm off by a slight amount due to the shifting and discretization though, so perhaps someone else, like you or dpb or Star, can correct it.
But you'll see spikes at close to +/- 30 and +/- 60 Hz.

Connectez-vous pour commenter.

Plus de réponses (3)

Image Analyst
Image Analyst le 15 Oct 2016
I think you can fix n by doing this:
% Find min locations
[~, a1] = min(ta)
[~, a2] = max(ta)
[~, b1] = min(tb)
[~, b2] = max(tb)
% Find overall min and max over both arrays.
n = min([a1, b1]) : max([a2, b2])
However the rest of the code is still messed up but I don't know what you intend to do since you chose not to add any comments to explain it. Comments in code are always good.
  1 commentaire
Asad Zubair
Asad Zubair le 16 Oct 2016
I want to add two sine waves of 30 and 60 hz having sampling frequency of 1khz.

Connectez-vous pour commenter.


partha pratim talukdar
partha pratim talukdar le 26 Août 2021
Considering two frequency tones fm1=10 Hz and fm2=20Hz, with corresponding amplitudes
Am1=2V and Am2=4V, show the modulated and demodulated waveforms. Use built in functions.

Lungani
Lungani le 16 Avr 2023
Modifié(e) : Lungani le 16 Avr 2023
t=0:0.1:100;
F1=30;
x=sin(2*pi*F1*t);
subplot(4,1,1);
plot(t,F1);
F2=50;
y=sin(2*pi*F2*t);
subplot(4,1,1);
plot(t,F2);
F3=70;
z=sin(2*pi*F3*t);
subplot(4,1,1);
plot(t,F3);
a=F1+F2+F3;
subplot(4,1,1);
plot(a)
i am generating the code with 3 frequencies, so i must add and multiple these frequencies
what i want to know is the code, am i write it right or not?
  1 commentaire
Image Analyst
Image Analyst le 16 Avr 2023
No, you need to plot the signal, not the frequency. Corrected code is below:
% Demo by Image Analyst to sum sine harmonics.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 16;
markerSize = 6;
numSamples = 1000;
t = linspace(0, 100, numSamples);
Frequency1 = 30;
y1 = sin(2*pi*Frequency1*t);
subplot(4,1,1);
plot(t, y1);
grid on;
title('y1', 'FontSize',fontSize);
Frequency2 = 50;
y2 = sin(2*pi*Frequency2*t);
subplot(4,1,2);
plot(t, y2);
grid on;
title('y2', 'FontSize',fontSize)
Frequency3 = 70;
y3 = sin(2*pi*Frequency3*t);
subplot(4,1,3);
plot(t, y3);
grid on;
title('y3', 'FontSize',fontSize)
all3y = y1 + y2 + y3;
subplot(4,1,4);
plot(all3y, 'b-')
grid on;
title('y1 + y2 + y3', 'FontSize',fontSize)

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by