Clear Filters
Clear Filters

Load data from .mat file to be used in other functions App designer

7 views (last 30 days)
Hi, I'm trying to load a HRIR data to my app to use the left and right data, I'm using a checkbox to allow selection for that,
% Value changed function: LargepinnaCheckBox
function LargepinnaCheckBoxValueChanged(app, event)
value = app.LargepinnaCheckBox.Value;
if value==1
load("large_pinna.mat");
end
end
I need to use the loaded data in my following function,
app.x1 = HRIRprocessing(app.x1,app.angle1,app.left,app.right);
where in this function it need the left and right channel data to run, I've also created public properties "left"and "right", but I'm kinda stuck where I am right now. thank you
  2 Comments
Isaac Zhu
Isaac Zhu on 2 Jun 2023
function output = my_fft_conv(signal1, signal2)
% Convolves two audio signals
% Obtain length of first signal
lensig1 = length(signal1);
% Obtain length of second signal
lensig2 = length(signal2);
% convolution process if using regular convolution
outputlength = lensig1 + lensig2 - 1; % Calculate length of the resulting
% Calculates output signal from multiplication of frequency responses
output = real(ifft(fft(signal1, outputlength) .* fft(signal2, outputlength)));
% Remove the samples of the impulse response from the end of the resulting
% signal
output = output(1:end-200,:);
here's my_fft_conv function
Isaac Zhu
Isaac Zhu on 2 Jun 2023
function outwave = HRIRprocessing(inwave, angle, left, right)
% This function processes a one-channel audio signal using the HRIR set
% from the CIPIC database (small pinna) for an azimuth angle.
%
% Input arguments:
% inwave - one-channel audio signal
% angle - azumuth angle in degrees in a range of 0 to 355 in steps of 5
%
% Output arguments:
% outwave - resulting two-channel audio signal
if angle < 0
angle = angle+360;
end
angle_idx = angle/5+1; % convert angle in degrees to index number
left_hrir = left(:,angle_idx); % extract the angle degrees HRIR
right_hrir = right(:,angle_idx); % extract the right angle degrees HRIR
l_signal = my_fft_conv(inwave, left_hrir); % convolve the audio signal with the HRIR for the left channel
r_signal = my_fft_conv(inwave, right_hrir); % convolve the audio signal with the HRIR for the right channel
outwave = [l_signal r_signal]; % concatenate the signals to crete a stereo signal
outwave = outwave./max(max(abs(outwave))); % normalise the resulting signal
sound(outwave,fs);
Here's the HRIRprocessing function and error returns:
Error using my_fft_conv
Arrays have incompatible sizes for this operation.
Error in HRIRprocessing (line 23)
l_signal = my_fft_conv(inwave, left_hrir); % convolve the audio signal with the HRIR for the left channel

Sign in to comment.

Accepted Answer

LeoAiE
LeoAiE on 2 Jun 2023
Based on the code snippet and your explanation, it seems like you are trying to load a .mat file when a checkbox in your GUI app is selected, and then use the loaded data in a subsequent function. The problem you might be facing is that the loaded data isn't being assigned to your "left" and "right" properties in your app.
You have to remember that when you use the load function in MATLAB, it loads the variables into the current workspace. When you are inside a function (as you are when you're in the callback for the checkbox value changed), the workspace is local to that function. Therefore, after you load the data, you need to assign the loaded data to your app's properties.
Assuming that the large_pinna.mat file contains two variables, left and right, you could modify the checkbox callback function as follows:
% Value changed function: LargepinnaCheckBox
function LargepinnaCheckBoxValueChanged(app, event)
value = app.LargepinnaCheckBox.Value;
if value==1
loadedData = load("large_pinna.mat");
app.left = loadedData.left;
app.right = loadedData.right;
end
end
In this way, you are loading the .mat file data into a structure (loadedData) and then assigning the 'left' and 'right' fields from that structure to your app's properties. Now, you can use app.left and app.right in your other functions as required.
  5 Comments
Isaac Zhu
Isaac Zhu on 2 Jun 2023
Hi, thanks for your help, I've added this two in my hrir function:
function outwave = HRIRprocessing(inwave, angle, left, right)
% This function processes a one-channel audio signal using the HRIR set
% from the CIPIC database (small pinna) for an azimuth angle.
%
% Input arguments:
% inwave - one-channel audio signal
% angle - azumuth angle in degrees in a range of 0 to 355 in steps of 5
%
% Output arguments:
% outwave - resulting two-channel audio signal
if angle < 0
angle = angle+360;
end
angle_idx = angle/5+1; % convert angle in degrees to index number
left_hrir = left(:,angle_idx); % extract the angle degrees HRIR
right_hrir = right(:,angle_idx); % extract the right angle degrees HRIR
len_diff = length(inwave) - length(left_hrir);
if len_diff > 0
% inwave is longer, pad left_hrir with zeros
left_hrir = [left_hrir; zeros(len_diff, 1)];
elseif len_diff < 0
% left_hrir is longer, pad inwave with zeros
inwave = [inwave; zeros(-len_diff, 1)];
end
if size(inwave, 1) ~= size(left_hrir, 1)
% Reshape them into column vectors
inwave = reshape(inwave, [], 1);
left_hrir = reshape(left_hrir, [], 1);
end
len_diff = length(inwave) - length(right_hrir);
if len_diff > 0
% inwave is longer, pad left_hrir with zeros
right_hrir = [right_hrir; zeros(len_diff, 1)];
elseif len_diff < 0
% left_hrir is longer, pad inwave with zeros
inwave = [inwave; zeros(-len_diff, 1)];
end
if size(inwave, 1) ~= size(right_hrir, 1)
% Reshape them into column vectors
inwave = reshape(inwave, [], 1);
right_hrir = reshape(right_hrir, [], 1);
end
l_signal = my_fft_conv(inwave, left_hrir); % convolve the audio signal with the HRIR for the left channel
r_signal = my_fft_conv(inwave, right_hrir); % convolve the audio signal with the HRIR for the right channel
outwave = [l_signal r_signal]; % concatenate the signals to crete a stereo signal
outwave = outwave./max(max(abs(outwave))); % normalise the resulting signal
and after I call this and trying to play back using sound(app.x1,app.fs), it shows error:
Error using sound
Only one- and two-channel audio supported.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!