conversion of sum of two sine wave from time domain to frequency domain

3 次查看(过去 30 天)
I am writing a code to convert the sum of two sine wave in matlab's appdesigner in which while plotting it is it is showing the plot when frequencies are changed but as soon as I change the value in amplitude slider from zero the graph is not being plotted. the complete code is below:
classdef app_5 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
PLOTButton matlab.ui.control.Button
a2Slider matlab.ui.control.Slider
a2SliderLabel matlab.ui.control.Label
a1Slider matlab.ui.control.Slider
a1SliderLabel matlab.ui.control.Label
f2Slider matlab.ui.control.Slider
f2SliderLabel matlab.ui.control.Label
f1Slider matlab.ui.control.Slider
f1SliderLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: f1Slider
function f1SliderValueChanged(app, event)
value = app.f2Slider.Value;
value = app.f1Slider.Value;
fr1= app.f1Slider.Value;
fr2= app.f2Slider.Value;
am1= app.a1Slider.Value;
am2= app.a2Slider.Value;
fs=1000;
t= 0:0.01:100*pi;
w1= 2*pi*fr1;
w2= 2*pi*fr2;
y= am1*sin(w1*t);
z= am2*sin(w2*t);
x= y+z;
number_of_samples=numel(t);
f=0:fs/number_of_samples:fs-fs/number_of_samples;
lx=numel(x);
xn_fft=fft(x,lx);
xf=abs(xn_fft);
m=numel(f);
for i=1:m
plot(app.UIAxes,f(1:i),xf(1:i));
drawnow;
end
end
% Value changed function: f2Slider
function f2SliderValueChanged(app, event)
value = app.f2Slider.Value;
value = app.f1Slider.Value;
fr1= app.f1Slider.Value;
fr2= app.f2Slider.Value;
am1= app.a1Slider.Value;
am2= app.a2Slider.Value;
fs=1000;
t= 0:0.01:100*pi;
w1= 2*pi*fr1;
w2= 2*pi*fr2;
y= am1*sin(w1*t);
z= am2*sin(w2*t);
x= y+z;
number_of_samples=numel(t);
f=0:fs/number_of_samples:fs-fs/number_of_samples;
lx=numel(x);
xn_fft=fft(x,lx);
xf=abs(xn_fft);
m=numel(f);
for i=1:m
plot(app.UIAxes,f(1:i),xf(1:i));
drawnow;
end
end
% Value changed function: a1Slider
function a1SliderValueChanged(app, event)
value = app.f2Slider.Value;
value = app.f1Slider.Value;
fr1= app.f1Slider.Value;
fr2= app.f2Slider.Value;
am1= app.a1Slider.Value;
am2= app.a2Slider.Value;
fs=1000;
t= 0:0.01:100*pi;
w1= 2*pi*fr1;
w2= 2*pi*fr2;
y= am1*sin(w1*t);
z= am2*sin(w2*t);
x= y+z;
number_of_samples=numel(t);
f=0:fs/number_of_samples:fs-fs/number_of_samples;
lx=numel(x);
xn_fft=fft(x,lx);
xf=abs(xn_fft);
m=numel(f);
for i=1:m
plot(app.UIAxes,f(1:i),xf(1:i));
drawnow;
end
end
% Value changed function: a2Slider
function a2SliderValueChanged(app, event)
value = app.f2Slider.Value;
value = app.f1Slider.Value;
fr1= app.f1Slider.Value;
fr2= app.f2Slider.Value;
am1= app.a1Slider.Value;
am2= app.a2Slider.Value;
fs=1000;
t= 0:0.01:100*pi;
w1= 2*pi*fr1;
w2= 2*pi*fr2;
y= am1*sin(w1*t);
z= am2*sin(w2*t);
x= y+z;
number_of_samples=numel(t);
f=0:fs/number_of_samples:fs-fs/number_of_samples;
lx=numel(x)
xn_fft=fft(x,lx);
xf=abs(xn_fft);
m=numel(f);
for i=1:m
plot(app.UIAxes,f(1:i),xf(1:i));
drawnow;
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.XLim = [0 10];
app.UIAxes.YLim = [-10 10];
app.UIAxes.XAxisLocation = 'origin';
app.UIAxes.Position = [1 1 640 270];
% Create f1SliderLabel
app.f1SliderLabel = uilabel(app.UIFigure);
app.f1SliderLabel.HorizontalAlignment = 'right';
app.f1SliderLabel.Position = [12 444 25 22];
app.f1SliderLabel.Text = 'f1';
% Create f1Slider
app.f1Slider = uislider(app.UIFigure);
app.f1Slider.Limits = [0 10];
app.f1Slider.ValueChangedFcn = createCallbackFcn(app, @f1SliderValueChanged, true);
app.f1Slider.Position = [58 453 150 3];
% Create f2SliderLabel
app.f2SliderLabel = uilabel(app.UIFigure);
app.f2SliderLabel.HorizontalAlignment = 'right';
app.f2SliderLabel.Position = [385 444 25 22];
app.f2SliderLabel.Text = 'f2';
% Create f2Slider
app.f2Slider = uislider(app.UIFigure);
app.f2Slider.Limits = [0 10];
app.f2Slider.ValueChangedFcn = createCallbackFcn(app, @f2SliderValueChanged, true);
app.f2Slider.Position = [431 453 150 3];
% Create a1SliderLabel
app.a1SliderLabel = uilabel(app.UIFigure);
app.a1SliderLabel.HorizontalAlignment = 'right';
app.a1SliderLabel.Position = [18 363 25 22];
app.a1SliderLabel.Text = 'a1';
% Create a1Slider
app.a1Slider = uislider(app.UIFigure);
app.a1Slider.Limits = [0 10];
app.a1Slider.ValueChangedFcn = createCallbackFcn(app, @a1SliderValueChanged, true);
app.a1Slider.Position = [64 372 150 3];
% Create a2SliderLabel
app.a2SliderLabel = uilabel(app.UIFigure);
app.a2SliderLabel.HorizontalAlignment = 'right';
app.a2SliderLabel.Position = [384 363 25 22];
app.a2SliderLabel.Text = 'a2';
% Create a2Slider
app.a2Slider = uislider(app.UIFigure);
app.a2Slider.Limits = [0 10];
app.a2Slider.ValueChangedFcn = createCallbackFcn(app, @a2SliderValueChanged, true);
app.a2Slider.Position = [430 372 150 3];
% Create PLOTButton
app.PLOTButton = uibutton(app.UIFigure, 'push');
app.PLOTButton.Position = [271 296 100 22];
app.PLOTButton.Text = 'PLOT';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app_5
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
The image of the app is attached with the post.
Please help me how can I rectify the problem
  3 个评论

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by