Errors in converting real time sine wave in it's real time Fourier transform. The graph in UI Axes for frequency domain is not getting plotted in real time

2 次查看(过去 30 天)
I have created an app in which two axes are used one for plotting sine wave in time domain and other in frequency. The wave is being ploteed in time domain but not in frequency domain. Please help me
the code is as follows:
classdef app_4 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
NoiseAmplitudeSlider matlab.ui.control.Slider
NoiseAmplitudeSliderLabel matlab.ui.control.Label
SinefrequencySlider matlab.ui.control.Slider
SinefrequencySliderLabel matlab.ui.control.Label
SineAmplitudeSlider matlab.ui.control.Slider
SineAmplitudeSliderLabel matlab.ui.control.Label
UIAxes2 matlab.ui.control.UIAxes
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: SineAmplitudeSlider
function SineAmplitudeSliderValueChanged(app, event)
value = app.SineAmplitudeSlider.Value;
f=app.SinefrequencySlider.Value;
a=app.SineAmplitudeSlider.Value;
w=2*pi*f;
na=app.NoiseAmplitudeSlider.Value;
t=0:0.01:100*pi;
x=a*sin(w*t);
nfft=100;
n=numel(t);
y=na*rand(1,length(t));
sine=x+y;
for i=1:n
plot(app.UIAxes,t(1:i),sine(1:i));
drawnow;
end
nfft=1024;
sine=sfft(x,nfft);
sine=sine(1:nfft/2);
mx=abs(sine);
fs=(0:nfft/2-1)*f/nfft;
m=numel(m);
for j=1:m
plot(app.UIAxes2,fs(1:j),mx(1:j));
drawnow;
end
end
% Value changed function: SinefrequencySlider
function SinefrequencySliderValueChanged(app, event)
value = app.SinefrequencySlider.Value;
f=app.SinefrequencySlider.Value;
a=app.SineAmplitudeSlider.Value;
w=2*pi*f;
na=app.NoiseAmplitudeSlider.Value;
t=0:0.01:100*pi;
x=a*sin(w*t);
nfft=100;
n=numel(t);
y=na*rand(1,length(t));
sine=x+y;
for i=1:n
plot(app.UIAxes,t(1:i),sine(1:i));
drawnow;
end
nfft=1024;
sine=sfft(x,nfft);
sine=sine(1:nfft/2);
mx=abs(sine);
fs=(0:nfft/2-1)*f/nfft;
m=numel(m);
for j=1:m
plot(app.UIAxes2,fs(1:j),mx(1:j));
drawnow;
end
end
% Value changed function: NoiseAmplitudeSlider
function NoiseAmplitudeSliderValueChanged(app, event)
value = app.NoiseAmplitudeSlider.Value;
f=app.SinefrequencySlider.Value;
a=app.SineAmplitudeSlider.Value;
w=2*pi*f;
na=app.NoiseAmplitudeSlider.Value;
t=0:0.01:100*pi;
x=a*sin(w*t);
nfft=100;
n=numel(t);
y=na*rand(1,length(t));
sine=x+y;
for i=1:n
plot(app.UIAxes,t(1:i),sine(1:i));
drawnow;
end
nfft=1024;
sine=sfft(x,nfft);
sine=sine(1:nfft/2);
mx=abs(sine);
fs=(0:nfft/2-1)*f/nfft;
m=numel(m);
for j=1:m
plot(app.UIAxes2,fs(1:j),mx(1:j));
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 790 489];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'TIME DOMAIN')
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.YAxisLocation = 'origin';
app.UIAxes.Position = [297 260 475 208];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Frequency Domain')
xlabel(app.UIAxes2, 'X')
ylabel(app.UIAxes2, 'Y')
zlabel(app.UIAxes2, 'Z')
app.UIAxes2.YLim = [-10 10];
app.UIAxes2.XAxisLocation = 'origin';
app.UIAxes2.YAxisLocation = 'origin';
app.UIAxes2.Position = [297 25 475 212];
% Create SineAmplitudeSliderLabel
app.SineAmplitudeSliderLabel = uilabel(app.UIFigure);
app.SineAmplitudeSliderLabel.HorizontalAlignment = 'right';
app.SineAmplitudeSliderLabel.Position = [1 429 86 22];
app.SineAmplitudeSliderLabel.Text = 'Sine Amplitude';
% Create SineAmplitudeSlider
app.SineAmplitudeSlider = uislider(app.UIFigure);
app.SineAmplitudeSlider.Limits = [0 10];
app.SineAmplitudeSlider.ValueChangedFcn = createCallbackFcn(app, @SineAmplitudeSliderValueChanged, true);
app.SineAmplitudeSlider.Position = [108 438 150 3];
% Create SinefrequencySliderLabel
app.SinefrequencySliderLabel = uilabel(app.UIFigure);
app.SinefrequencySliderLabel.HorizontalAlignment = 'right';
app.SinefrequencySliderLabel.Position = [1 352 86 22];
app.SinefrequencySliderLabel.Text = 'Sine frequency';
% Create SinefrequencySlider
app.SinefrequencySlider = uislider(app.UIFigure);
app.SinefrequencySlider.Limits = [0 10];
app.SinefrequencySlider.ValueChangedFcn = createCallbackFcn(app, @SinefrequencySliderValueChanged, true);
app.SinefrequencySlider.Position = [108 361 150 3];
% Create NoiseAmplitudeSliderLabel
app.NoiseAmplitudeSliderLabel = uilabel(app.UIFigure);
app.NoiseAmplitudeSliderLabel.HorizontalAlignment = 'right';
app.NoiseAmplitudeSliderLabel.Position = [1 289 126 22];
app.NoiseAmplitudeSliderLabel.Text = 'Noise Amplitude Slider';
% Create NoiseAmplitudeSlider
app.NoiseAmplitudeSlider = uislider(app.UIFigure);
app.NoiseAmplitudeSlider.Limits = [0 10];
app.NoiseAmplitudeSlider.ValueChangedFcn = createCallbackFcn(app, @NoiseAmplitudeSliderValueChanged, true);
app.NoiseAmplitudeSlider.Position = [148 298 150 3];
% 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_4
% 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
end
I have bold the code made by me. Please do let me know what is the error.
  2 个评论
Anupriya Bhardwaj
Anupriya Bhardwaj 2021-12-7
I have mentioned that I have two UI Axes and in first one time domain is being plotted but in second one after using fft function the graph is not being plotted. So, it will be a great help because I am not able to find my mistake.

请先登录,再进行评论。

回答(1 个)

prabhat kumar sharma
Hi Anupriya,
I understand you are facing issue in frequency domain plotting section. Specifically, there are a couple of mistakes that need to be corrected for the frequency domain plot to work correctly. Here's a breakdown of the issues and how to fix them:
  • Incorrect Function for FFT: You've used sfft(x,nfft) which seems to be a typo or an undefined function. The correct MATLAB function for computing the Fast Fourier Transform (FFT) is fft.'
For More information on fourier tranform please refer the below documentation:
  • Incorrect Handling of m Variable: You have a line m=numel(m); which doesn't make sense because m is not defined before this line. You likely intended to find the number of elements in another variable, possibly mx or fs.
  • Plotting the Frequency Domain Data: The loop for plotting the frequency domain data seems unnecessary and might cause the plot to update very slowly, especially with drawnow inside the loop. Instead, you can plot the entire dataset at once.
Here's the refrence code for the corrected section of your code for the SineAmplitudeSliderValueChanged function, which should also be applied to the other slider value changed functions (SinefrequencySliderValueChanged, NoiseAmplitudeSliderValueChanged):
% Value changed function: SineAmplitudeSlider
function SineAmplitudeSliderValueChanged(app, event)
f = app.SinefrequencySlider.Value;
a = app.SineAmplitudeSlider.Value;
w = 2 * pi * f;
na = app.NoiseAmplitudeSlider.Value;
t = 0:0.01:100 * pi;
x = a * sin(w * t);
nfft = 1024; % FFT points
y = na * rand(1, length(t));
sine = x + y;
plot(app.UIAxes, t, sine); % Plot in time domain
% Correct FFT usage
Y = fft(sine, nfft);
Y = Y(1:nfft/2); % Take the first half of FFT result
mx = abs(Y);
fs = (0:nfft/2 - 1) * (1 / (2 * pi)) * f / (nfft / 2); % Correct frequency scale
plot(app.UIAxes2, fs, mx); % Plot in frequency domain
end
I hope it will help you to resolve your issue.

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by