0% found this document useful (0 votes)
0 views

Multimedia Lab 1

This lab report from Noakhali Science and Technology University details various text compression techniques implemented in a programming context, including Run-Length Encoding, Differential Encoding, Static and Dynamic Huffman Coding, Arithmetic Coding, LZ Coding, and Lempel-Ziv-Welch Coding. Each technique is explained theoretically and accompanied by code implementations demonstrating their functionality. The report serves as a practical exploration of multimedia communication principles within the Information & Communication Engineering curriculum.

Uploaded by

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

Multimedia Lab 1

This lab report from Noakhali Science and Technology University details various text compression techniques implemented in a programming context, including Run-Length Encoding, Differential Encoding, Static and Dynamic Huffman Coding, Arithmetic Coding, LZ Coding, and Lempel-Ziv-Welch Coding. Each technique is explained theoretically and accompanied by code implementations demonstrating their functionality. The report serves as a practical exploration of multimedia communication principles within the Information & Communication Engineering curriculum.

Uploaded by

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

Noakhali Science and Technology

University
Information & Communication
Engineering
Lab Report

Course Name: Multimedia Communication Lab


Course Code:ICE-4206

Submitted By: Submitted To:


Name: Mohibur Rahaman Sabbir Md. Mahbubul Alam

Roll No: ASH2011029M Assistant Professor


Year- 4, Term- 2 Department of ICE
Session:2019-20 Noakhali Science and technology
University
Department of ICE
Experiment No: 01
Name of Experiment: A program to implement different text compression techniques.
Theoretical Background:
Run-Length Encoding:
Run-Length Encoding (RLE) is a simple form of data compression where consecutive elements
that are identical are replaced with a single element followed by the count of repetitions. This
encoding is particularly useful for compressing data that contains long sequences of repeated
elements.
Differential Encoding:
Differential encoding is a technique used in information theory and signal processing to encode
data by representing the difference between sequential data points rather than the absolute
values. This encoding method is often employed to reduce redundancy in data transmission and
storage.
Static Huffman Coding:
Huffman coding is a widely used compression algorithm in information theory. It is a variable-
length encoding technique that assigns shorter codes to more frequent symbols and longer codes
to less frequent symbols. Huffman encoding is a lossless compression algorithm, meaning that
the original data can be perfectly reconstructed from the compressed data.
Dynamic Huffman Coding:
Dynamic Huffman Coding is an adaptive version of Huffman coding that updates the frequency
table and Huffman tree dynamically as each symbol is processed. Unlike static Huffman, it
requires only one pass through the data, making it suitable for streaming applications. The
encoder and decoder maintain synchronized trees, adjusting codewords as symbol frequencies
change. This adaptability makes it effective for data with varying symbol distributions but adds
complexity due to the need to update and transmit tree modifications.
Arithmetic Coding:
Arithmetic coding is a type of entropy encoding utilized in lossless data compression. Ordinarily,
a string of characters, for example, the words "hey" is represented for utilizing a fixed number of
bits per character. In the most straightforward case, the probability of every symbol occurring is
equivalent. For instance, think about a set of three symbols, A, B, and C, each similarly prone to
happen. Straightforward block encoding would require 2 bits for every symbol, which is
inefficient as one of the bit varieties is rarely utilized. In other words, A = 00, B = 01, and C =
10, however, 11 is unused. An increasingly productive arrangement is to speak to a succession of
these three symbols as a rational number in base 3 where every digit speaks to a symbol. For
instance, the arrangement "ABBCAB" could become 0.011201. In arithmetic coding as an
incentive in the stretch [0, 1).
LZ Coding (Lempel-Ziv):
LZ Coding, based on the Lempel-Ziv algorithms (e.g., LZ77, LZ78), is a dictionary-based
compression method that identifies and replaces repeated substrings with references to earlier
occurrences. In LZ77, a sliding window is used to find matches in the recent past, encoding them
as (offset, length, next symbol) triples. LZ78 builds a dictionary of encountered substrings,
encoding data as (dictionary index, next symbol) pairs. LZ coding excels at compressing text
with repetitive patterns, such as natural language or code, and forms the basis for many modern
compression tools like gzip.
Lempel Ziv Welch Coding:
Lempel-Ziv-Welch (LZW) is a lossless compression algorithm that is widely used in multimedia
communication and file compression. LZW achieves compression by replacing repeated
sequences of characters with a single code. The algorithm dynamically builds a dictionary of
sequences encountered during encoding, updating it as new sequences are encountered.
Code Implementation:
1. Run Length
Program:
#include <bits/stdc++.h>
using namespace std;

void printRLE(string str)


{
int n = str.length();
for (int i = 0; i < n; i++) {

// Count occurrences of current character


int count = 1;
while (i < n - 1 && str[i] == str[i + 1]) {
count++;
i++;
}
// Print character and its count
cout <<"(" <<str[i] <<","<< count<<")";
}
}
//Driver code
int main()
{
string str;
cout<<"Enter a string"<<endl;
cin>>str;
printRLE(str);
return 0;
}

Output:

2. Differential Encoding
Program:
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

#define MAX_LEN 1000

// Function to perform differential encoding


vector<int> differentialEncode(const char input[]) {
vector<int> encoded;
int len = strlen(input);

if (len == 0) return encoded;

// Store ASCII value of the first character


encoded.push_back((int)input[0]);

// Store differences
for (int i = 1; i < len; ++i) {
int diff = (int)input[i] - (int)input[i - 1];
encoded.push_back(diff);
}

return encoded;
}

// Function to decode the encoded differences


void differentialDecode(const vector<int>& encoded, char output[]) {
if (encoded.empty()) {
output[0] = '\0';
return;
}

// First character is the base


output[0] = (char)encoded[0];
int prev = encoded[0];

for (size_t i = 1; i < encoded.size(); ++i) {


int current = prev + encoded[i];
output[i] = (char)current;
prev = current;
}

output[encoded.size()] = '\0'; // Null-terminate


}

int main() {
char text[MAX_LEN];

cout << "Enter text to encode: ";


cin.getline(text, MAX_LEN);

// Encoding
vector<int> encoded = differentialEncode(text);

cout << "\nDifferential Encoded Output:\n";


for (int value : encoded) {
cout << value << " ";
}
cout << endl;

// Decoding
char decodedText[MAX_LEN];
differentialDecode(encoded, decodedText);

cout << "\nDecoded Text:\n" << decodedText << endl;

return 0;
}

Output:
3. Static Huffman Encoding:
Program:
#include <iostream>
#include <string>
#include <iomanip>

#define MAX 100

using namespace std;

// Huffman tree node


struct Node {
char symbol;
float prob;
Node* left;
Node* right;
};

// Create a new node


Node* createNode(char symbol, float prob, Node* left = nullptr, Node* right = nullptr) {
Node* node = new Node();
node->symbol = symbol;
node->prob = prob;
node->left = left;
node->right = right;
return node;
}

// Swap function
void swap(Node*& a, Node*& b) {
Node* temp = a;
a = b;
b = temp;
}

// Bubble sort by probability


void sortNodes(Node* nodes[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (nodes[j]->prob > nodes[j + 1]->prob) {
swap(nodes[j], nodes[j + 1]);
}
}
}
}

// Recursive function to generate Huffman codes


void generateCodes(Node* root, string code, string codes[256]) {
if (!root) return;

if (!root->left && !root->right) {


codes[(unsigned char)root->symbol] = code;
cout << "Symbol: " << root->symbol
<< " | Probability: " << fixed << setprecision(3) << root->prob
<< " | Code: " << code << endl;
return;
}

generateCodes(root->left, code + "0", codes);


generateCodes(root->right, code + "1", codes);
}

// Free memory allocated for the Huffman tree


void freeTree(Node* root) {
if (!root) return;
freeTree(root->left);
freeTree(root->right);
delete root;
}

int main() {
int n;
cout << "Enter the number of symbols: ";
cin >> n;

if (n <= 1 || n > MAX) {


cout << "Invalid number of symbols. Must be between 2 and " << MAX << "." << endl;
return 1;
}

Node* nodes[MAX];
bool symbolUsed[256] = {false};
float totalProb = 0;
// Input symbols and their probabilities
for (int i = 0; i < n; i++) {
char sym;
float prob;

cout << "Enter symbol " << i + 1 << ": ";


cin >> ws >> sym; // ws skips whitespace

if (symbolUsed[(unsigned char)sym]) {
cout << "Duplicate symbol! Try again." << endl;
i--;
continue;
}

symbolUsed[(unsigned char)sym] = true;

cout << "Enter probability for '" << sym << "': ";
cin >> prob;

if (prob <= 0 || prob > 1) {


cout << "Probability must be > 0 and ≤ 1." << endl;
return 1;
}

totalProb += prob;
if (totalProb > 1.001) {
cout << "Total probability exceeds 1.0. Try again." << endl;
return 1;
}

nodes[i] = createNode(sym, prob);


}

// Build Huffman tree


int size = n;
while (size > 1) {
sortNodes(nodes, size);
Node* left = nodes[0];
Node* right = nodes[1];
Node* merged = createNode('\0', left->prob + right->prob, left, right);

for (int i = 2; i < size; i++) {


nodes[i - 2] = nodes[i];
}
nodes[size - 2] = merged;
size--;
}

Node* root = nodes[0];

// Generate Huffman codes


string codes[256];
cout << "\nHuffman Codes:\n";
generateCodes(root, "", codes);

// Input message to encode


cin.ignore(); // Clear newline from buffer
string message;
cout << "\nEnter a message to encode using the above symbols: ";
getline(cin, message);

// Encode the message


string encoded = "";
for (char c : message) {
if (codes[(unsigned char)c].empty()) {
cout << "Error: Symbol '" << c << "' was not in the input set.\n";
freeTree(root);
return 1;
}
encoded += codes[(unsigned char)c];
}

cout << "\nFinal Encoded Binary String:\n" << encoded << endl;

// Cleanup
freeTree(root);
return 0;
}
Output:
4. Dynamic Huffman Encoding:
Program:
#include<bits/stdc++.h>

using namespace std;


// Node for the Huffman Tree
struct Node {
char symbol;
int weight;
int number;
Node *parent, *left, *right;
Node(char s = '\0', int w = 0, int num = 0)
: symbol(s), weight(w), number(num), parent(nullptr), left(nullptr), right(nullptr) {}
};

// Tree class to handle dynamic Huffman operations


class DynamicHuffman {
private:
Node* root;
Node* NYT; // Not Yet Transmitted node
unordered_map<char, Node*> symbolTable;
int maxNumber;

// Generate binary code by traversing to root


string getCode(Node* node) {
string code = "";
Node* current = node;
while (current->parent != nullptr) {
if (current == current->parent->left)
code = '0' + code;
else
code = '1' + code;
current = current->parent;
}
return code;
}

// Update the tree with a symbol


void updateTree(Node* node) {
while (node != nullptr) {
node->weight++;
node = node->parent;
}
}
public:
DynamicHuffman() {
root = new Node();
NYT = root;
root->number = 512;
maxNumber = 511;
}

// Encode a single character


string encodeChar(char c) {
string code = "";

if (symbolTable.find(c) == symbolTable.end()) {
// First time seeing this character
code = getCode(NYT); // Send NYT path
code += bitset<8>(c).to_string(); // Send raw 8-bit char

// Create new internal node and two children


Node* internal = new Node('\0', 0, maxNumber--);
Node* newLeaf = new Node(c, 1, maxNumber--);

internal->left = NYT;
internal->right = newLeaf;
internal->parent = NYT->parent;

if (NYT->parent) {
if (NYT->parent->left == NYT)
NYT->parent->left = internal;
else
NYT->parent->right = internal;
} else {
root = internal;
}

NYT->parent = internal;
newLeaf->parent = internal;

symbolTable[c] = newLeaf;

updateTree(internal->parent); // update from new internal upward


} else {
// Existing symbol
Node* current = symbolTable[c];
code = getCode(current);
updateTree(current);
}
return code;
}

string encode(const string& text) {


string result = "";
for (char c : text) {
result += encodeChar(c);
}
return result;
}
};
int main() {
string input;
cout << "Enter text for Dynamic Huffman Encoding: ";
getline(cin, input);
DynamicHuffman encoder;
string encoded = encoder.encode(input);
cout << "\nEncoded Binary Stream:\n" << encoded << "\n";

return 0;
}

Output:

5. Arithmetic Coding:
Program:
#include <bits/stdc++.h>
using namespace std;

// Structure to store character probability range


struct SymbolRange {
double low;
double high;
};
// Function to calculate frequencies and probabilities
map<char, SymbolRange> calculateProbabilities(const string& text) {
map<char, int> freq;
for (char c : text) {
freq[c]++;
}

int total = text.length();


map<char, SymbolRange> ranges;
double cumulative = 0.0;

for (auto& pair : freq) {


double probability = (double)pair.second / total;
ranges[pair.first] = { cumulative, cumulative + probability };
cumulative += probability;
}

return ranges;
}

// Function to perform arithmetic encoding


double arithmeticEncode(const string& text, map<char, SymbolRange>& ranges) {
double low = 0.0;
double high = 1.0;

for (char c : text) {


double range = high - low;
high = low + range * ranges[c].high;
low = low + range * ranges[c].low;
}

// You can return any number between low and high


return (low + high) / 2;
}

int main() {
string text;

cout << "Enter text to compress using Arithmetic Encoding: ";


getline(cin, text);

if (text.empty()) {
cout << "Empty input." << endl;
return 0;
}
map<char, SymbolRange> ranges = calculateProbabilities(text);

cout << fixed << setprecision(10);


cout << "\nCharacter Ranges (based on probabilities):\n";
for (auto& pair : ranges) {
cout << "'" << pair.first << "' : [" << pair.second.low << ", " << pair.second.high <<
")\n";
}

double encodedValue = arithmeticEncode(text, ranges);


cout << "\nEncoded Value (compressed): " << encodedValue << endl;

return 0;
}

Output:

6. LZ Encoding:

Program:

#include <bits/stdc++.h>
using namespace std;

// Structure to represent an encoded triple: (offset, length, next character)


struct LZTuple {
int offset;
int length;
char nextChar;
};
// Function to find the longest match in the search buffer
LZTuple findLongestMatch(const string& data, int pos, int windowSize) {
int maxLength = 0;
int offset = 0;
char nextChar = data[pos];

int start = max(0, pos - windowSize);

for (int i = start; i < pos; ++i) {


int length = 0;
while (i + length < pos && pos + length < data.size() &&
data[i + length] == data[pos + length]) {
length++;
}

if (length > maxLength) {


maxLength = length;
offset = pos - i;
nextChar = (pos + length < data.size()) ? data[pos + length] : '\0';
}
}

return { offset, maxLength, nextChar };


}

// LZ77 Encoding function


vector<LZTuple> lz77Encode(const string& input, int windowSize = 20) {
vector<LZTuple> encoded;
int i = 0;

while (i < input.size()) {


LZTuple match = findLongestMatch(input, i, windowSize);
encoded.push_back(match);
i += match.length + 1;
}

return encoded;
}

// Display encoded output


void displayEncoded(const vector<LZTuple>& encoded) {
cout << "\nLZ77 Encoded Output (offset, length, nextChar):\n";
for (const auto& t : encoded) {
cout << "(" << t.offset << ", " << t.length << ", ";
if (t.nextChar == '\0')
cout << "EOF";
else
cout << "'" << t.nextChar << "'";
cout << ")\n";
}
}

int main() {
string input;
cout << "Enter text to compress using LZ77: ";
getline(cin, input);

vector<LZTuple> compressed = lz77Encode(input);

displayEncoded(compressed);

return 0;
}

Output:

7. LZW Encoding:

Program:

#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>

using namespace std;

// LZW Encoding Function


vector<int> lzwEncode(const string& input) {
unordered_map<string, int> dictionary;
vector<int> encodedOutput;

// Initialize dictionary with single character strings


for (int i = 0; i < 256; i++) {
dictionary[string(1, i)] = i;
}
string current = "";
int code = 256; // Next available code

for (char ch : input) {


string next = current + ch;
if (dictionary.find(next) != dictionary.end()) {
current = next;
} else {
encodedOutput.push_back(dictionary[current]);
dictionary[next] = code++;
current = string(1, ch);
}
}

if (!current.empty()) {
encodedOutput.push_back(dictionary[current]);
}

return encodedOutput;
}

int main() {
string input;
cout << "Enter text to compress using LZW: ";
getline(cin, input);

vector<int> encoded = lzwEncode(input);

cout << "\nLZW Encoded Output (Codewords):\n";


for (int code : encoded) {
cout << code << " ";
}
cout << endl;

return 0;
}

Output:
Discussion:

RLE is best for highly repetitive text but limited elsewhere. Differential Encoding suits
numerical sequences within text. Static Huffman is efficient for known distributions, while
Dynamic Huffman adapts to changing ones. Arithmetic Coding offers superior compression but
is complex. LZ and LZW shine in general-purpose text compression, with LZW being simpler
but less flexible. In practice, LZ-based methods dominate modern tools (e.g., ZIP), while
Huffman and Arithmetic Coding are used in specialized contexts like JPEG or PDF. Combining
these methods (e.g., LZ with Huffman) often yields optimal results for diverse text data.

Experiment No: 02
Experiment Name: Implementation of Fundamental MATLAB Functions for Image
Processing and Analysis.

Theoretical Background:

Image processing is a fundamental area in digital signal processing and computer vision,
enabling the manipulation and analysis of visual data to extract meaningful information or
enhance image quality. MATLAB provides a powerful and user-friendly environment for
implementing and testing a wide range of image processing functions.

In this lab, various built-in MATLAB functions were used to perform key image processing
tasks. Each function plays a significant role in different stages of the image processing pipeline:

 Basic Functions like imread, imshow, and imwrite are essential for loading, displaying,
and saving images. These form the foundation for any image manipulation task.
 Preprocessing Functions such as imresize, imcrop, imrotate, imfilter, and imgaussfilt help
in preparing images for analysis by resizing, rotating, blurring, or enhancing them.
 Color and Format Conversion Functions like im2gray, rgb2gray, and im2bw are used to
convert images into suitable formats, especially grayscale, for further processing like
edge detection or segmentation.
 Analysis and Feature Extraction Functions such as imhist, edge, hough, impixel, and
bwconncomp are crucial for analyzing image content, detecting edges, counting objects,
and studying textures.
 Transform and Frequency Domain Functions like dct2 and idct2 allow for image
representation in the frequency domain, which is essential in image compression and
filtering applications.
 Advanced Feature Functions like gabor and imgaborfilt are used in texture segmentation
and feature extraction, particularly in applications like pattern recognition.
 Utility Functions such as montage, imsave, and immovie assist in visualization, saving,
and creating image sequences or animations.
Each of these functions plays a unique role in enabling efficient, accurate, and flexible image
processing workflows. Understanding and applying them is critical for solving real-world
problems in medical imaging, remote sensing, machine vision, and multimedia applications.

Implementation:

1) Imshow

Description: Displays an image in a figure window.


Usage: Visualization of image data.

Code:
img = imread('rgb.jpg');
imshow(img);

Output:

2) Imread
Description: Reads an image from a file into the MATLAB workspace.
Usage: Input image for processing.
Code:
Img = imread(‘image_name.png’);

3) Imresize
Description: Resizes an image to a specified size.
Usage: Scale image dimensions up or down.
Code:
img = imread('rgb.png');
resizedImg = imresize(img, 0.5); % Resize to 50%
imshow(resizedImg);

Output:

4) Imcrop
Description: Crops a Rectangular portion of an image.
Usage: Extract region of interest (ROI).
Code:
img = imread('rgb.jpg');
croppedImg = imcrop(img, [100, 100, 150, 150]); % [x y width height]
imshow(croppedImg);

5) Imwrite
Description: Writes an image to a file.
Usage: Save processed images.

Code:
img = imread('image_name.png');
imwrite(img, 'saved_image.png');

6) Imfinfo
Description: Retrieves metadata about an image file.
Usage: Image properties (size, type, etc.).
Code:
info = imfinfo('rgb.png');
disp(info);
Output:

7) im2gray
Description: Converts an RGB image to grayscale.
Usage: Simplify processing, especially for edge detection, etc.

Code:
img = imread('rgb.png');
grayImg = im2gray(img);
imshow(grayImg);

8) rgb2gray
Description: Older function to convert RGB to grayscale.
Usage: Similar to im2gray
Code:
img = imread('peppers.png');
grayImg = rgb2gray(img);
imshow(grayImg);

9) imhist
Description: Displays histogram of image pixel intensities.
Usage: Analyze contrast, brightness, and dynamic range.
Code:
img = rgb2gray(imread('peppers.png'));
imhist(img);

10) im2frame
Description: Converts image to a movie frame.
Usage: Create animations or videos.
Code:
img = imread('peppers.png');
frame = im2frame(img);

11) edge
Description: Detects edges using various methods (e.g., Sobel, Canny).
Usage: Feature detection, segmentation.
Code:
img = rgb2gray(imread('peppers.png'));
edges = edge(img, 'Canny');
imshow(edges);

12) hough
Description: Performs Hough transform to detect lines.
Usage: Line detection in edge-detected images.
Code:
img = edge(rgb2gray(imread('peppers.png')), 'Canny');
[H, theta, rho] = hough(img);
imshow(H, [ ]);

13) impixel
Description: Returns pixel values from specified coordinates.
Usage: Inspect pixel intensity manually.
Code:
img = imread('peppers.png');
imshow(img);
val = impixel(img, 100, 100);
disp(val);
14) bwconncomp
Description: Finds connected components in binary image.
Usage: Object counting, segmentation.
Code:
bw = im2bw(imread('coins.png'));
cc = bwconncomp(bw);
disp(cc.NumObjects);

15) immse
Description: Computes Mean Squared Error (MSE) between two images.
Usage: Image quality evaluation.
Code:
img1 = imread('peppers.png');
img2 = imresize(img1, 0.5);
img2 = imresize(img2, size(img1(:,:,1)));
mse = immse(img1, img2);
disp(mse);

16) dct2
Description: Computes 2D Discrete Cosine Transform.
Usage: Frequency analysis, JPEG compression.
Code:
img = rgb2gray(imread('peppers.png'));
dctImg = dct2(img);
imshow(log(abs(dctImg)), []);
17) idct2
Description: Computes 2D Inverse DCT.
Usage: Reconstruct image from DCT.
Code:
reconstructedImg = idct2(dctImg);
imshow(uint8(reconstructedImg));

18) imfilter
Description: Applies custom filter to image.
Usage: Blurring, sharpening, edge detection.
Code:
img = rgb2gray(imread('peppers.png'));
h = fspecial('sobel');
filtered = imfilter(img, h);
imshow(filtered);

19) imgaussfilt
Description: Applies Gaussian smoothing filter.
Usage: Noise reduction.

Code:
img = imread('peppers.png');
blurred = imgaussfilt(img, 2);
imshow(blurred);

20) gabor
Description: Creates Gabor filter.
Usage: Texture analysis, feature extraction.
Code:
g = gabor(4, 90); % wavelength 4, orientation 90 degrees
21) imgaborfilt
Description: Applies Gabor filter to image.
Usage: Texture segmentation, edge orientation.
Code:
img = rgb2gray(imread('peppers.png'));
g = gabor(4, 90);
filteredImg = imgaborfilt(img, g);
imshow(filteredImg, []);

22) immovie
Description: Creates movie from frame array.
Usage: Video creation.
Code:
img = imread('peppers.png');
frames = repmat(im2frame(img), 1, 10);
movie(frames);
23) montage
Description: Displays multiple images as a montage.
Usage: Compare or visualize many images.

Code:
img1 = imread('peppers.png');
img2 = imresize(img1, 0.5);
montage({img1, img2});

24) imsave
Description: Opens a dialog box to save the displayed image.
Usage: Manual image saving via GUI.
Code:
img = imread('peppers.png');
imshow(img);
imsave;

25) imrotate
Description: Rotates an image by specified angle.
Usage: Data augmentation, alignment.
Code:
img = imread('peppers.png');
rotated = imrotate(img, 45);
imshow(rotated);

26) imerase
Description: Erases parts of an image using a mask.
Usage: Region removal.
Code:
img = imread('peppers.png');
mask = false(size(img(:,:,1)));
mask(100:200, 100:200) = true;
result = imerase(img, mask);
imshow(result);

Discussion:
In this experiment, we explored a wide range of MATLAB functions used in image processing,
gaining practical experience with tools essential for manipulating, analyzing, and enhancing
digital images. Through functions like imread, imshow, imresize, imfilter, and edge, we learned
how to load, visualize, modify, and extract important features from images. The experiment
highlighted the importance of each function in different stages of image processing, from basic
operations to advanced feature extraction. Overall, this hands-on practice has provided a strong
foundation for understanding digital image processing techniques and their applications in real-
world scenarios.
Experiment No: 03
Experiment Name: A program to implement different functions on an audio.
Theoretical background:

Multimedia communication involves the integration of various media types, including audio.
Audio processing is a critical aspect that encompasses tasks like recording, playback, and
retrieving information about audio files. The provided Python script demonstrates audio
operations, including recording, playing/pausing music, and retrieving information about an
audio file.

Code Implementation and Explanation:

1. audioread
Description: audioread is used to read audio data from a file (such as .wav, .mp3, or .flac). It
returns the audio samples and the sample rate.
Usage: Load an audio signal into MATLAB for processing or analysis.
Code:
[y, Fs] = audioread('audiofile.wav'); % y: audio signal, Fs: sample rate
sound(y, Fs); % play the audio

2. audiowrite
Description: audiowrite writes audio data to a file. It's useful for saving modified or
generated audio signals.
Usage: Save processed audio signals to a file.
Code:
audiowrite('audio1.mp3', y, Fs); % Write signal y to a new file

3. sound
Description: sound plays an audio signal in the MATLAB environment. It sends the signal to
the default audio output device.
Usage: Listen to audio data directly from MATLAB.
Code:
sound(y, Fs); % Play audio signal y at sampling rate Fs
4. fft
Description: fft (Fast Fourier Transform) converts a time-domain signal to its frequency-
domain representation. It’s crucial for analyzing the frequency content of audio signals.
Usage: Frequency analysis, noise reduction, filtering.
Code:
y = y(:,1); % Use one channel if stereo
Y = fft(y); % Frequency-domain representation
f = (0:length(Y)-1)*Fs/length(Y); % Frequency vector
plot(f, abs(Y)); title('Frequency Spectrum');
xlabel('Frequency (Hz)'); ylabel('|Amplitude|');
5. filter
Description: filter applies a digital filter to the audio signal using specified coefficients. It’s
used for signal enhancement, noise reduction, and equalization.
Usage: Apply low-pass, high-pass, or custom-designed filters.
Code:
[b, a] = butter(4, 0.4, 'low'); % 4th-order low-pass Butterworth filter
filtered_audio = filter(b, a, y);
sound(filtered_audio, Fs); % Play filtered signal

Discussion:
In this experiment, we explored key MATLAB functions used in audio signal processing,
including audioread, audiowrite, sound, fft, and filter. These functions allowed us to load,
play, analyze, and manipulate audio data effectively. By applying transformations and filters,
we gained a practical understanding of time and frequency domain processing. This
experiment reinforced fundamental concepts in digital audio processing and demonstrated
how MATLAB serves as a powerful tool for real-time audio analysis, enhancement, and
modification.

You might also like