Skip to content

Commit df44005

Browse files
committedOct 17, 2023
Matlab project on signal operation in signal and system
0 parents  commit df44005

25 files changed

+323
-0
lines changed
 

‎ssProject/StarWars3.wav

129 KB
Binary file not shown.

‎ssProject/StarWars60.wav

129 KB
Binary file not shown.

‎ssProject/adio.wav

413 KB
Binary file not shown.

‎ssProject/adio1.wav

413 KB
Binary file not shown.

‎ssProject/adio2.wav

413 KB
Binary file not shown.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function extracted_watermark = audio_watermark_extraction(watermarked_audio_file, watermark_length, host)
2+
3+
[w_sig, ~] = audioread(watermarked_audio_file);
4+
5+
[a_sig, ~] = audioread(host);
6+
7+
extracted_watermark = w_sig(1:watermark_length) - a_sig(1:watermark_length);
8+
9+
extracted_watermark = extracted_watermark > 0.5;
10+
11+
end
12+
13+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function extracted_watermark = audio_watermark_extraction_emd(processed, host, watermark_length)
2+
[sig1, ~] = audio_read(processed);
3+
residual_1 = sig1;
4+
[sig2, ~] = audio_read(host);
5+
residual_2 = sig2;
6+
7+
imf1 = residual_1 - mean(residual_1);
8+
imf2 = residual_1 - mean(residual_2);
9+
10+
extracted_watermark = imf2(1:watermark_length, 1) - imf1(1:watermark_length, 1);
11+
end

‎ssProject/audio_watermarking.m

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function sig = audio_watermarking(host_audio_file, output_file, watermark_length)
2+
3+
[host_audio, sample_rate] = audioread(host_audio_file);
4+
5+
watermark = generate_watermark(watermark_length);
6+
7+
watermarked_audio = host_audio;
8+
9+
watermark = reshape(watermark, size(watermarked_audio(1:watermark_length)));
10+
11+
watermarked_audio(1:watermark_length) = watermarked_audio(1:watermark_length) + watermark;
12+
13+
audiowrite(output_file, watermarked_audio, sample_rate);
14+
sig = host_audio;
15+
end

‎ssProject/audio_watermarking_emd.m

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function processed_audio = audio_watermarking_emd(host_audio, watermark, output_file)
2+
3+
[audio_sig, sample_rate] = audioread(host_audio);
4+
fs = length(audio_sig)/sample_rate;
5+
audio_sig = audio_sig(:);
6+
imfs = []; imf = [];
7+
residual = audio_sig;
8+
while any(residual)
9+
imf = residual - mean(residual);
10+
padding = zeros(length(imf) - length(watermark));
11+
watermark_padded = [watermark padding];
12+
imf = imf + watermark_padded;
13+
[~, h] = freqz(imf, 1, fs);
14+
if abs(h(1)) > abs(h(end))
15+
imf = -imf;
16+
end
17+
imfs = [imfs imf];
18+
residual = residual - imf;
19+
end
20+
21+
signal = zeros(length(imfs(1)), 1);
22+
23+
for i = 1:length(imfs)
24+
signal = signal + imfs(i);
25+
end
26+
27+
processed_audio = audiowrite(output_file, signal, sample_rate);
28+
29+
end

‎ssProject/emdOptions.m

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
classdef emdOptions %#codegen
2+
%EMDOPTIONS Create option class for the EMD command
3+
%
4+
% OPT = emdOptions returns the default options for EMD.
5+
%
6+
% OPT = emdOptions('Properties1',Value1,'Properties2',Value2,...) uses
7+
% name/value pairs to override the default values for 'Properties1','Properties2',...
8+
% This function is only for internal use
9+
10+
% Copyright 2017 The MathWorks, Inc.
11+
12+
properties (Constant)
13+
defaultInterpolation = 'spline';
14+
end
15+
16+
properties (Access = public)
17+
Interpolation
18+
SiftStopCriterion = struct('SiftRelativeTolerance', 0.2, 'SiftMaxIterations', 100);
19+
DecompStopCriterion = struct('MaxNumIMF', 10, 'MaxNumExtrema', 1, 'MaxEnergyRatio', 20);
20+
Display = 0;
21+
end
22+
23+
methods
24+
%----------------------------------------------------------------------
25+
function this = emdOptions(varargin)
26+
if(~isempty(varargin))
27+
if coder.target('MATLAB')
28+
this = this.parseInputsMatlab(varargin{:});
29+
else
30+
this = this.parseInputsCodegen(varargin{:});
31+
end
32+
else
33+
% assign default values
34+
this.Interpolation = this.defaultInterpolation;
35+
end
36+
end
37+
%----------------------------------------------------------------------
38+
function [this] = parseInputsMatlab(this, varargin)
39+
p = inputParser;
40+
addParameter(p,'Interpolation', this.defaultInterpolation);
41+
addParameter(p,'SiftRelativeTolerance', this.SiftStopCriterion.SiftRelativeTolerance);
42+
addParameter(p,'SiftMaxIterations', this.SiftStopCriterion.SiftMaxIterations);
43+
addParameter(p,'MaxNumIMF', this.DecompStopCriterion.MaxNumIMF);
44+
addParameter(p,'MaxNumExtrema', this.DecompStopCriterion.MaxNumExtrema);
45+
addParameter(p,'MaxEnergyRatio', this.DecompStopCriterion.MaxEnergyRatio);
46+
addParameter(p,'Display', this.Display);
47+
48+
% parse input
49+
if(mod(length(varargin),2)~=0)
50+
error(message('shared_signalwavelet:emd:general:unpairedNameValue'));
51+
else
52+
parse(p,varargin{:});
53+
end
54+
55+
% assign value
56+
fieldNames = fields(p.Results);
57+
for i = 1:length(fields(p.Results))
58+
if(isfield(this.DecompStopCriterion, fieldNames{i}))
59+
this.DecompStopCriterion.(fieldNames{i}) = p.Results.(fieldNames{i});
60+
elseif(isfield(this.SiftStopCriterion, fieldNames{i}))
61+
this.SiftStopCriterion.(fieldNames{i}) = p.Results.(fieldNames{i});
62+
else
63+
this.(fieldNames{i}) = p.Results.(fieldNames{i});
64+
end
65+
end
66+
end
67+
%----------------------------------------------------------------------
68+
function [this] = parseInputsCodegen(this, varargin)
69+
defaultSiftRelativeTolerance = this.SiftStopCriterion.SiftRelativeTolerance;
70+
defaultSiftMaxIterations = this.SiftStopCriterion.SiftMaxIterations;
71+
defaultMaxNumIMF = this.DecompStopCriterion.MaxNumIMF;
72+
defaultMaxNumExtrema = this.DecompStopCriterion.MaxNumExtrema;
73+
defaultMaxEnergyRatio = this.DecompStopCriterion.MaxEnergyRatio;
74+
defaultDisplay = this.Display;
75+
76+
parms = struct( 'Interpolation', uint32(0), ...
77+
'SiftRelativeTolerance', uint32(0), ...
78+
'SiftMaxIterations', uint32(0), ...
79+
'MaxNumIMF', uint32(0), ...
80+
'MaxNumExtrema', uint32(0),...
81+
'MaxEnergyRatio', uint32(0),...
82+
'Display', uint32(0));
83+
84+
pstruct = eml_parse_parameter_inputs(parms,[],varargin{:});
85+
this.Interpolation = eml_get_parameter_value(pstruct.Interpolation,...
86+
this.defaultInterpolation,varargin{:});
87+
this.SiftStopCriterion.SiftRelativeTolerance = eml_get_parameter_value( pstruct.SiftRelativeTolerance,...
88+
defaultSiftRelativeTolerance,varargin{:});
89+
this.SiftStopCriterion.SiftMaxIterations = eml_get_parameter_value( pstruct.SiftMaxIterations,...
90+
defaultSiftMaxIterations,varargin{:});
91+
this.DecompStopCriterion.MaxNumIMF = eml_get_parameter_value( pstruct.MaxNumIMF,...
92+
defaultMaxNumIMF,varargin{:});
93+
this.DecompStopCriterion.MaxNumExtrema = eml_get_parameter_value( pstruct.MaxNumExtrema,...
94+
defaultMaxNumExtrema,varargin{:});
95+
this.DecompStopCriterion.MaxEnergyRatio = eml_get_parameter_value( pstruct.MaxEnergyRatio,...
96+
defaultMaxEnergyRatio,varargin{:});
97+
this.Display = eml_get_parameter_value( pstruct.Display, defaultDisplay,varargin{:});
98+
end
99+
end
100+
%----------------------------------------------------------------------
101+
methods(Static)
102+
function props = matlabCodegenNontunableProperties(classname)
103+
props = {'Interpolation'};
104+
end
105+
end
106+
end
107+
108+
109+
110+
111+
112+
113+
114+
115+

‎ssProject/generate_watermark.m

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function watermark = generate_watermark(length)
2+
watermark = randi([0, 1], 1, length);
3+
end

‎ssProject/identify_owner.m

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function ownerInfo = identify_owner(output, mappingFile, host_file, len)
2+
3+
watermark = audio_watermark_extraction(output, len, host_file);
4+
5+
watermark_str = num2str(watermark);
6+
watermark_str = reshape(watermark_str, 1, []);
7+
load(mappingFile, 'person_watermark_map');
8+
9+
person_ids = keys(person_watermark_map);
10+
watermarks = values(person_watermark_map);
11+
12+
for i = 1:length(watermarks)
13+
14+
watermark1 = strrep(watermarks{i}, ' ', '');
15+
watermark2 = strrep(watermark_str, ' ', '');
16+
17+
18+
19+
if strcmp(watermark1, watermark2)
20+
ownerInfo = person_ids{i};
21+
disp('Owner Information:');
22+
disp(ownerInfo);
23+
return;
24+
end
25+
end
26+
27+
disp('No matching owner found for the watermarked audio.');
28+
ownerInfo = '';
29+
end

‎ssProject/load_mapfile.m

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function load_mapfile()
2+
mapping_file = 'watermark_mapping.mat';
3+
load(mapping_file, 'person_watermark_map');
4+
5+
keys = person_watermark_map.keys;
6+
values = person_watermark_map.values;
7+
8+
disp('Person-Watermark Mapping:');
9+
for i = 1:length(keys)
10+
disp(['Person ID: ', keys{i}]);
11+
disp('Watermark:');
12+
for j = 1:numel(values{i})
13+
fprintf('%s ', num2str(values{i}(j)));
14+
end
15+
disp(' ');
16+
disp('-------------------------');
17+
end
18+
end

‎ssProject/main_driver.m

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
len = 50;
2+
3+
mode = input('Enter the mode (search or store): ', 's');
4+
5+
if strcmp(mode, 'search')
6+
7+
[host_file, input_path] = uigetfile('*.wav', 'Select the input audio file');
8+
9+
[output, output_path] = uigetfile('*.wav', 'Enter the output audio file name');
10+
11+
sig1 = audio_watermarking(host_file, output, len);
12+
sig2 = audio_watermark_extraction(output, len, host_file);
13+
person_watermark_mapping(prop, sig2);
14+
identify_owner(outstoreput, 'watermark_mapping.mat',host_file, len);
15+
%load_mapfile();
16+
17+
elseif strcmp(mode, 'store')
18+
prop = input('Enter Owner Name: ', 's');
19+
20+
21+
[host_file, input_path] = uigetfile('*.wav', 'Select the input audio file');
22+
23+
24+
[output, output_path] = uigetfile('*.wav', 'Enter the output audio file name');
25+
26+
sig1 = audio_watermarking(host_file, output, len);
27+
sig2 = audio_watermark_extraction(output, len, host_file);
28+
person_watermark_mapping(prop, sig2);
29+
identify_owner(output, 'watermark_mapping.mat', host_file, len);
30+
load_mapfile();
31+
subplot(3,1,1);
32+
plot(sig1);
33+
title('Host signal');
34+
grid;
35+
subplot(3,1,2);
36+
plot(sig2);
37+
title('Watermark signal');
38+
axis([0, 100, -2, 2])
39+
grid;
40+
subplot(3,1,3);
41+
plot(audioread(output));
42+
title('Output File');
43+
grid;
44+
45+
end

‎ssProject/person_watermark_mapping.m

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function person_watermark_mapping(person_id, watermark)
2+
3+
mapping_file = 'watermark_mapping.mat';
4+
5+
if exist(mapping_file, 'file') == 2
6+
load(mapping_file, 'person_watermark_map');
7+
8+
watermark_str = num2str(watermark);
9+
10+
person_watermark_map(person_id) = watermark_str;
11+
12+
else
13+
person_watermark_map = containers.Map('KeyType','char','ValueType','char');
14+
15+
watermark_str = num2str(watermark);
16+
17+
person_watermark_map(person_id) = watermark_str;
18+
end
19+
20+
save(mapping_file, 'person_watermark_map');
21+
22+
disp('Person-watermark mapping saved successfully.');
23+
end

‎ssProject/pr1.m

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
host_file = 'adio.wav';
2+
prop = 'adsad';
3+
output = 'adio1.wav';
4+
len = 50;
5+
sig1 = audio_watermarking(host_file, output, len);
6+
sig2 = audio_watermark_extraction(output, len, host_file);
7+
person_watermark_mapping(prop, sig2);
8+
identify_owner('w1.wav', 'watermark_mapping.mat', 'w.wav', len);
9+
load_mapfile();
10+
subplot(3,1,1);
11+
plot(sig1);
12+
title('Host signal');
13+
grid;
14+
subplot(3,1,2);
15+
plot(sig2);
16+
title('Watermark signal');
17+
axis([0, 100, -2, 2])
18+
grid;
19+
subplot(3,1,3);
20+
plot(audioread(output));
21+
title('Ouput File');
22+
grid;

‎ssProject/saad.wav

1.08 MB
Binary file not shown.

‎ssProject/sample-12s.wav

413 KB
Binary file not shown.

‎ssProject/sample-3s.wav

551 KB
Binary file not shown.

‎ssProject/sample-6s.wav

1.08 MB
Binary file not shown.

‎ssProject/sample-9s.wav

1.61 MB
Binary file not shown.

‎ssProject/taunt.wav

2.15 MB
Binary file not shown.

‎ssProject/umar.wav

1.61 MB
Binary file not shown.

‎ssProject/watermark_mapping.mat

780 Bytes
Binary file not shown.

‎ssProject/zaid.wav

1.61 MB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.