0% found this document useful (0 votes)
6 views4 pages

DSP Exp6

The document outlines a procedure for performing spectral analysis and generating spectrograms for sine waves, square waves, and audio files using MATLAB. It explains the theory behind Fourier Transform and Fast Fourier Transform (FFT), and provides detailed MATLAB code for generating and plotting the waveforms and their frequency representations. The results include visualizations of the time-domain signals, their frequency spectra, and spectrograms showing frequency content over time.

Uploaded by

pskiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

DSP Exp6

The document outlines a procedure for performing spectral analysis and generating spectrograms for sine waves, square waves, and audio files using MATLAB. It explains the theory behind Fourier Transform and Fast Fourier Transform (FFT), and provides detailed MATLAB code for generating and plotting the waveforms and their frequency representations. The results include visualizations of the time-domain signals, their frequency spectra, and spectrograms showing frequency content over time.

Uploaded by

pskiran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

6.

SPECTRAL ANALYSIS AND SPECTROGRAM


AIM: To perform Spectral Analysis of given Waveforms. and Plot Spectrogram.
a. Sine b. Square c. Audio file
APPARATUS: Operating system: Windows 7
Software: MATLAB 8.1
THEORY: The Fourier Transform is a mathematical technique used to transform a signal
from the time domain into the frequency domain. It helps us see the frequency components
of a signal.Fast Fourier Transform (FFT) is an efficient algorithm to compute the FT for
discrete signals.
A spectrogram is a time-frequency representation of a signal, showing how the frequency
content changes over time. Since a sine wave is stationary (its frequency content does not
change over time), the spectrogram will show a single horizontal line at the frequency of the
sine wave. The spectrogram of a square wave will show a series of peaks at the fundamental
frequency and its odd harmonics, appearing as discrete lines across the frequency axis. Since
square waves are periodic and do not change over time, their spectrogram will show constant
frequency components.
PROCEDURE:
1.Open MATLAB window and create a new program in M – file.
2.Save the program.
3.Compile the program to check for errors.
4.Run the program and view the output graph on the screen
PROGRAM:
A. Sine Wave
Clc;
Clear all;
Close all;
% Parameters for the sine wave
fs = 1000; % Sampling frequency (Hz)
T = 1; % Duration of the signal (seconds)
f_sine = 50; % Frequency of the sine wave (Hz)
t = 0:1/fs:T-1/fs; % Time vector

% Generate the sine wave


y = sin(2 * pi * f_sine * t);

% Plot the sine wave


figure;
subplot(3,1,1);
plot(t, y);
title('Sine Wave');
xlabel('Time (seconds)');
ylabel('Amplitude');

% Perform Spectral Analysis using FFT


N = length(y); % Number of points in FFT
Y = fft(y); % Fast Fourier Transform
f = (0:N-1)*(fs/N); % Frequency vector
Y_mag = abs(Y)/N; % Magnitude of FFT
Y_mag = Y_mag(1:N/2); % Take the positive frequencies

subplot(3,1,2);
plot(f(1:N/2), Y_mag); % Plot the magnitude spectrum
title('Spectral Analysis (FFT)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

% Generate Spectrogram
subplot(3,1,3);
spectrogram(y, 256, 200, 256, fs, 'yaxis'); % Spectrogram plot
title('Spectrogram');
colorbar;
B. SQUARE WAVE
% Parameters
Fs = 1000; % Sampling frequency in Hz
T = 1; % Signal duration in seconds
f_square = 50; % Frequency of square wave in Hz
t = 0:1/Fs:T-1/Fs; % Time vector

% Generate Square Wave


square_wave = square(2*pi*f_square*t);

% Plot Square Wave


figure;
subplot(3,1,1);
plot(t, square_wave);
title('Square Wave');
xlabel('Time (seconds)');
ylabel('Amplitude');

% Spectral analysis (FFT)


N = length(square_wave); % Length of signal
f = (0:N-1)*(Fs/N); % Frequency vector
Square_FFT = abs(fft(square_wave)); % FFT of square wave
% Plot Spectral Analysis (FFT)
subplot(3,1,2);
plot(f, Square_FFT);
title('Spectral Analysis of Square Wave (FFT)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
xlim([0 100]); % Focus on relevant frequencies (0 to 100 Hz)

% Spectrogram of Square Wave


subplot(3,1,3);
spectrogram(square_wave, 256, 250, 256, Fs, 'yaxis');
title('Spectrogram of Square Wave'); % Parameters

C.AUDIO FILE
PROGRAM:

clc;
clear all;
close all;
% Read the audio file
[audio, fs] = audioread('sample.wav');
% Plot the audio input graph
t = (0:length(audio)-1)/fs;
figure;
plot(t, audio);
title('Audio Input Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Perform the Fast Fourier Transform (FFT)
n = length(audio);
f = (0:n-1)*(fs/n);
audio_fft = abs(fft(audio));
% Plot the spectral analysis
figure;
plot(f(1:n/2), audio_fft(1:n/2));
title('Spectral Analysis');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% Plot the spectrogram
figure;
spectrogram(audio(:,1), 256, 250, 256, fs, 'yaxis');
title('Spectrogram');

You might also like