function write_vector_to_file_for_DSP(x, outputFilename) %write_vector_to_file_for_DSP(x, outputFilename) %this script takes a vector input of values that are assumed to have %magnitude less than 1 and outputs these values to a file that is formatted %for TI DSP assembly code (for cutting/pasting into assembly). %The function will scale the values to 16-bit 2's complement integers by %multiplying by 2^15 before writing to the output file. if nargin < 2 fprintf('Syntax: write_vector_to_file_for_DSP(x, outputFilename)\n'); fprintf('\tMust specify input vector x to be written to a \n'); fprintf('\tfile specified by the string name in outputFilename'); return; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Error-checking stuff % check amplitude maxAmplitude = max(abs(x)); if maxAmplitude > 1 fprintf('Maximum amplitude of input must not exceed 1!'); return; end % check dimensions (verify that input is a vector [m, n] = size(x); if (m > 1) && (n > 1) fprintf('Input must be a vector (either row or a column)'); return; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% x = round(x * (2^15-1)); % scale fractions to 16-bit integers vals_per_line = 8; full_lines = floor(length(x) / vals_per_line); label = inputname(1); label2 = ''; startIndex = 1; f = fopen(outputFilename, 'w'); for k = 1 : full_lines endIndex = startIndex + vals_per_line - 1; if length(label) ~= 0 label2 = sprintf('%s%d', label, (k-1) * vals_per_line); %pad with up to 5 spaces (for up to 5-digit numbers) label2 = [label2, ' ' * ones(1, length(label) + 5 - length(label2))]; end fprintf(f, '%s\t .word \t', label2); fprintf(f, '%d, ', x(startIndex:endIndex-1)); fprintf(f, '%d \r\n', x(endIndex)); startIndex = startIndex + vals_per_line; end left_over = length(x) - startIndex + 1; if (left_over > 0) if length(label) ~= 0 label2 = sprintf('%s%d', label, full_lines * vals_per_line); %pad with up to 5 spaces (for up to 5-digit numbers) label2 = [label2, ' ' * ones(1, length(label) + 5 - length(label2))]; end fprintf(f, '%s\t .word \t', label2); fprintf(f, '%d, ', x(startIndex:end-1)); fprintf(f, '%d \r\n', x(end)); end fclose(f);