Taking DFT of Swept Sine Wave

2 views (last 30 days)
I have been having Problem in Taking DFT of a Swept Sine Signal, my code is below, please Review the Code and Let me give some Suggestions if true amp=4;
t1=str2num(get(handles.t1,'String'));
t2=str2num(get(handles.t2,'String'));
dt=str2num(get(handles.interval,'String'));
t=t1:dt:t2;
ValF=str2num(get(handles.startf,'String'));
ValF1=str2num(get(handles.endf,'String'));
f1=2*pi*ValF;
f2=2*pi*ValF1;
f= ((f2-f1).*t/(t2-t1)) + f1;
% Define the time vector
y = amp .* sin(f .* t); % Compute y(t), the sine wave
plot(t,y) % Plot y vs. t
xlabel('Time (seconds)') % Label x-axis
ylabel('y(t)') % Label y-axis
title('Time-Frequency Plot') % Give plot a title
grid on % Turn on plot grid
diary off % Turn diary off
guidata(hObject,handles);
% code
function fourr(handles)
amp=4;
t1=str2num(get(handles.t1,'String'));
t2=str2num(get(handles.t2,'String'));
dt=str2num(get(handles.interval,'String'));
t=t1:dt:t2;
ValF=str2num(get(handles.startf,'String'));
ValF1=str2num(get(handles.endf,'String'));
f1=2*pi*ValF;
f2=2*pi*ValF1;
f= ((f2-f1).*t/(t2-t1)) + f1;
y = amp .* sind(f .* t);
z=stem(y)'
fft(z);
stem(f,fft(z));
xlabel('Time(sec)')
ylabel('FFT')
function ffft_Callback(hObject, eventdata, handles)
fourr(handles); end

Accepted Answer

Greg Heath
Greg Heath on 26 Jan 2013
Please do not use the acronym 'DFT' when you are using the 'FFT'
close all,clear all, clc, plt = 0;
N = 48
T = 3*pi
dt = T/N
t = dt*(0:N-1);
Fs = 1/dt
df = Fs/N
f = df*(0:N-1);
A =4
y = A * sin(f .* t);
plt=plt+1,figure(plt)
hold on
plot( t, y, 'LineWidth', 2)
plot( t, zeros(1,N), 'k--', 'LineWidth', 2 )
xlabel('Time (seconds)')
ylabel('y(t)')
title( 'SWEPT FREQUENCY TIME SIGNAL' )
Y = fftshift(fft(y))/N;
fb = f-Fs/2;
realY = real(Y);
imagY = imag(Y);
absY = abs(Y);
phaseY = angle(Y);
plt=plt+1,figure(plt)
subplot(2,2,1)
hold on
plot( fb, realY, 'LineWidth', 2 )
plot( fb, zeros(1,N), 'k--', 'LineWidth', 2 )
xlim( [ -Fs/2 Fs/2 ] )
ylabel( ' REAL(Y) ' )
title([ blanks(85) , ' SPECTRUM OF SWEPT FREQUENCY SIGNAL ' ] )
subplot(2,2,2)
hold on
plot( fb, imagY, 'LineWidth', 2 )
plot( fb, zeros(1,N), 'k--', 'LineWidth', 2 )
xlim( [ -Fs/2 Fs/2 ] )
ylabel( ' IMAG(Y) ' )
subplot(2,2,3)
hold on
plot( fb, absY, 'LineWidth', 2)
xlim( [ -Fs/2 Fs/2 ] )
ylabel( ' AMPLITUDE(Y) ' )
xlabel( ' FREQUENCY(HZ) ' ) ;
subplot(2,2,4)
hold on
plot(fb,phaseY, 'LineWidth', 2)
plot( fb, zeros(1,N), 'k--', 'LineWidth', 2 )
xlim( [ -Fs/2 Fs/2 ] )
ylabel( ' PHASE(Y) ' )
xlabel( ' FREQUENCY(HZ) ' ) ;
Hope this helps.
Thank you for formally accepting my answer.
Greg

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!