Linguistics and Language Studies Using MATLAB
MATLAB is a powerful tool for analyzing linguistic data and processing natural language. It provides a suite of tools and functions to work with text, process audio, and visualize linguistic patterns effectively. This section explores several use cases and examples related to linguistics and language studies using MATLAB.
Basic Text Processing
Text data can be processed in MATLAB using strings, character arrays, and cell arrays. Here’s how to perform basic operations:
% Basic text processing
sentence = "MATLAB is a tool for linguistic analysis.";
words = split(sentence); % Splitting into words
numWords = length(words); % Counting words
disp(words);
disp("Number of words: " + numWords);
Output:
words = "MATLAB" "is" "a" "tool" "for" "linguistic" "analysis" Number of words: 7
Phonetic Analysis
Analyzing phonetic patterns can be performed by processing audio files. MATLAB’s audio processing toolbox is useful here:
% Phonetic analysis
[audio, fs] = audioread('speech.wav'); % Read an audio file
time = (0:length(audio)-1) / fs; % Time vector
plot(time, audio);
xlabel('Time (s)');
ylabel('Amplitude');
title('Waveform of Speech');
The plot displays the waveform of the speech signal, providing insights into its amplitude and duration.
Word Frequency Analysis
Word frequency analysis helps identify patterns in text. Here’s an example:
% Word frequency analysis
text = "MATLAB is great for text analysis. Text analysis is a key part of NLP.";
words = split(lower(text)); % Convert to lowercase and split
uniqueWords = unique(words); % Find unique words
counts = zeros(size(uniqueWords));
for i = 1:length(uniqueWords)
counts(i) = sum(words == uniqueWords(i));
end
disp(table(uniqueWords, counts, 'VariableNames', {'Word', 'Frequency'}));
Sample Output:
Word Frequency MATLAB 1 analysis 2 for 1 is 2 ...
Text Similarity and Distance Metrics
Calculating similarity between sentences or words is a crucial part of language studies:
% Text similarity using Levenshtein distance
word1 = "matlab";
word2 = "matlab";
distance = editDistance(word1, word2); % Custom function for Levenshtein distance
disp("Edit Distance: " + distance);
Edit Distance Function
function d = editDistance(s1, s2)
len1 = length(s1);
len2 = length(s2);
dp = zeros(len1+1, len2+1);
for i = 1:len1+1
dp(i,1) = i-1;
end
for j = 1:len2+1
dp(1,j) = j-1;
end
for i = 2:len1+1
for j = 2:len2+1
cost = (s1(i-1) ~= s2(j-1));
dp(i,j) = min([dp(i-1,j)+1, dp(i,j-1)+1, dp(i-1,j-1)+cost]);
end
end
d = dp(len1+1, len2+1);
end
Advanced Example Problems: Speech-to-Text Using MATLAB
Speech-to-text conversion involves processing audio signals, extracting features, and mapping them to text. MATLAB provides powerful tools for implementing and experimenting with speech-to-text algorithms. Below are advanced examples and problems to explore.
Example 1: Speech Signal Preprocessing
Preprocessing speech signals is a critical step in speech-to-text systems. This involves noise reduction, normalization, and framing.
% Load and preprocess speech signal
[audio, fs] = audioread('speech_sample.wav'); % Load audio file
audio = audio / max(abs(audio)); % Normalize amplitude
audio = highpass(audio, 300, fs); % Remove low-frequency noise
audio = lowpass(audio, 3000, fs); % Remove high-frequency noise
time = (0:length(audio)-1) / fs;
plot(time, audio);
xlabel('Time (s)');
ylabel('Amplitude');
title('Preprocessed Speech Signal');
Output: A cleaned and normalized waveform ready for feature extraction.
Example 2: Extracting MFCC Features
Mel Frequency Cepstral Coefficients (MFCCs) are widely used in speech recognition for feature extraction:
% Extract MFCC features
audio = audioread('speech_sample.wav');
window = hamming(256); % Hamming window
overlap = 128; % Overlap size
nCoefficients = 13; % Number of MFCC coefficients
mfccs = mfcc(audio, fs, 'Window', window, 'OverlapLength', overlap, 'NumCoeffs', nCoefficients);
imagesc(mfccs');
axis xy;
xlabel('Frame');
ylabel('MFCC Coefficients');
title('MFCC Features');
The output is a spectrogram-like visualization of MFCC features, which can be used as input for machine learning models.
Example 3: Building a Simple Speech-to-Text Classifier
A supervised learning approach for speech-to-text involves training a classifier on labeled audio data.
% Train a simple speech-to-text classifier
load('speechDataset.mat'); % Load dataset with features and labels
X = dataset.Features; % Feature matrix (e.g., MFCCs)
Y = dataset.Labels; % Labels (e.g., words)
classifier = fitcecoc(X, Y); % Train multi-class SVM classifier
% Predict on new data
newAudio = audioread('new_sample.wav');
newFeatures = extractFeatures(newAudio, fs); % Extract features (e.g., MFCCs)
predictedWord = predict(classifier, newFeatures);
disp("Predicted Word: " + predictedWord);
This example uses MFCCs for feature extraction and a multi-class SVM for classification. Replace extractFeatures
with a feature extraction method like MFCC.
Example 4: Performing Real-Time Speech Recognition
Real-time speech recognition requires capturing audio from a microphone and processing it on the fly.
% Real-time speech recognition
recorder = audiorecorder(16000, 16, 1); % Initialize audio recorder
disp('Start speaking...');
recordblocking(recorder, 5); % Record for 5 seconds
disp('Recording stopped. Processing...');
audio = getaudiodata(recorder); % Get recorded audio
features = mfcc(audio, 16000, 'NumCoeffs', 13); % Extract MFCCs
predictedWord = predict(classifier, features); % Predict using trained model
disp("Predicted Word: " + predictedWord);
This script records speech for 5 seconds, extracts features, and predicts the spoken word in real time.
Example 5: Visualizing Spoken Word Spectrogram
Visualizing a spectrogram helps understand the frequency components of speech signals:
% Spectrogram of a spoken word
[audio, fs] = audioread('spoken_word.wav');
spectrogram(audio, 256, 200, 512, fs, 'yaxis');
title('Spectrogram of Spoken Word');
colorbar;
The spectrogram shows the frequency distribution of the spoken word over time.
Example 6: Dynamic Time Warping for Speech Comparison
DTW is used to align two speech signals of different lengths to find the best match. This is useful for speaker verification or phoneme comparison.
% Comparing two speech signals using DTW
[audio1, fs1] = audioread('speech1.wav');
[audio2, fs2] = audioread('speech2.wav');
% Resample to the same sampling rate if needed
if fs1 ~= fs2
audio2 = resample(audio2, fs1, fs2);
end
% Extract MFCC features
mfcc1 = mfcc(audio1, fs1);
mfcc2 = mfcc(audio2, fs1);
% Perform DTW
[dist, ix, iy] = dtw(mfcc1, mfcc2);
% Plot alignment
figure;
plot(ix, iy, 'LineWidth', 2);
xlabel('Indices in Signal 1');
ylabel('Indices in Signal 2');
title('Dynamic Time Warping Alignment');
disp("DTW Distance: " + dist);
The DTW distance provides a quantitative measure of similarity between two speech signals. The alignment plot shows the mapping of time indices.
Example 7: Hidden Markov Model (HMM) for Word Recognition
HMMs are ideal for recognizing sequences in speech, such as words or phonemes. MATLAB’s HMM toolbox can be used for this purpose.
% Train an HMM for word recognition
load('speechDataset.mat'); % Load a dataset of features and labels
features = dataset.Features; % Feature matrix (e.g., MFCCs)
labels = dataset.Labels; % Labels corresponding to words
% Split into training and testing data
[trainFeatures, testFeatures, trainLabels, testLabels] = splitData(features, labels);
% Train HMMs for each word
uniqueLabels = unique(trainLabels);
hmms = struct();
for i = 1:length(uniqueLabels)
word = uniqueLabels{i};
wordFeatures = trainFeatures(strcmp(trainLabels, word), :);
hmms.(word) = hmmtrain(wordFeatures, 5, 'Gaussian'); % 5 hidden states
end
% Test HMMs
for i = 1:length(testLabels)
word = testLabels{i};
testSample = testFeatures(i, :);
scores = struct();
for j = 1:length(uniqueLabels)
wordModel = uniqueLabels{j};
scores.(wordModel) = hmmprob(hmms.(wordModel), testSample);
end
[~, predictedWord] = max(struct2array(scores));
disp("True Word: " + word + ", Predicted Word: " + uniqueLabels{predictedWord});
end
This example demonstrates training HMMs for each word in a dataset and using them for recognition on test samples. Replace hmmtrain
and hmmprob
with specific HMM implementation methods.
Useful MATLAB Functions for Linguistics
Practice Questions
Test Yourself
1. Write a MATLAB script to calculate the frequency of each word in a text paragraph.
2. Create a custom function to calculate the cosine similarity between two word vectors.
3. Perform sentiment analysis using a dataset of positive and negative words.
4. Write a script to preprocess an audio file and extract MFCC features.
5. Train a speech-to-text classifier using a dataset of spoken digits (0-9).
6. Implement a system to capture audio in real time and classify spoken words using a pre-trained model.